我正在编写一个简单的程序,当按下按钮时显示图像。我对 Qt 很陌生,而且我没有任何运气来确定我的问题发生在哪里。
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() 不是两个的 SLOT。奇怪的是,它并没有给我同样的警告。我运行时没有填充任何小部件,因此,我不确定连接是否正常工作。
任何帮助表示赞赏,谢谢。