버튼을 누르면 이미지를 표시하는 간단한 프로그램을 작성하고 있습니다. Qt를 처음 접했을 때 문제가 발생한 곳을 확인하지 못했습니다.QPushButton 신호를 통해 QLabel setPixmap() 변경
class ImageSwitcher : public QWidget
{
Q_OBJECT
public:
ImageSwitcher();
QPushButton *leftButton;
QPushButton *rightButton;
~ImageSwitcher();
private slots:
void switchImages(QPixmap display);
private:
QLabel *canvas;
QPixmap *one;
QPixmap *two;
};
클래스 선언 :
ImageSwitcher::ImageSwitcher (void) {
canvas = new QLabel;
one = new QPixmap;
two = new QPixmap;
leftButton = new QPushButton("&One");
rightButton = new QPushButton("&Two");
one->load("one.png");
two->load("two.png");
//Close the program if the images cannot be loaded.
//Load the images to the QPixmaps.
//Connect the left and right buttons.
QObject::connect(leftButton, SIGNAL(clicked()), canvas, SLOT(switchImages(*one)));
QObject::connect(rightButton, SIGNAL(clicked()), canvas, SLOT(switchImages(*two)));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(canvas);
layout->addWidget(leftButton);
layout->addWidget(rightButton);
QWidget window;
window.setLayout(layout);
}
void ImageSwitcher::switchImages(QPixmap display) {
canvas->setPixmap(display);
}
ImageSwitcher::~ImageSwitcher (void) {
delete canvas;
delete one;
delete two;
delete leftButton;
delete rightButton;
}
그리고 마지막으로 주요 기능은 :
int main (int args, char **argv)
{
QApplication app(args, argv);
ImageSwitcher test;
test.show();
return app.exec();
}
나는 데 첫 번째 문제는 내 레이아웃 설정이 잘못이라는 것이다. 두 번째로, 명령 행은 switchImages()가 2에 대한 SLOT이 아니라고 경고합니다. 이상하게도, 그것은 나에게 같은 경고를주지 않습니다. 내가 실행할 때 어떤 위젯도 채워지지 않으므로 연결이 전혀 작동하는지 잘 모르겠습니다.
감사합니다. 감사합니다.
수동으로 SLOT에 인수를 넣을 수 없습니다. 신호로부터의 인수는 슬롯으로 전달된다. 즉. 신호와 슬롯은 같은 인수를 가져야합니다. –