当用户单击按钮时,我正在尝试执行操作。我的代码是:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtUiTools/QUiLoader>
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
createActions();
}
void MainWindow::test()
{
//QMessageBox::information(this, "Welcome", "Select first image.");
//QFileDialog::getOpenFileName(this, QT_TR_NOOP("Open Image"), "D:\\", QT_TR_NOOP("Image Files (*.png *.jpg *.bmp)"));
resize(100,500);
}
void MainWindow::createActions()
{
QWidget *centralWidget = this->centralWidget();
QPushButton *buttonBack = centralWidget->findChild<QPushButton *>("pushButton");
QObject::connect(buttonBack,SIGNAL(clicked()), this, SLOT(test()));
QAction *open = this->findChild<QAction *>("actionOpen");
//QMessageBox::information(this, "Welcome", open->text());
connect(open, SIGNAL(triggered()), this, SLOT(test()));
}
函数void MainWindow::test()
在头文件中定义为 SLOT,我敢肯定,这QPushButton *buttonBack
不为空。我做错了什么?
在我的代码中,我还尝试通过 执行操作QAction
,但在这种情况下,当我关闭窗口时会执行功能。