私はちょうどQTを学び始めました。達成したいのは、ボタンをクリックするとポップアップメッセージが表示されます。ここ は私のファイルがどのように見えるかです:QTメッセージボックスがボタンクリックでpopusにならない
main.cppに
#include "mainwindow.h"
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow *mainWindow = new MainWindow();
QLabel *text = new QLabel("Some text");
QPushButton *btn = new QPushButton("Click");
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(btn);
layout->addWidget(text);
QObject::connect(btn, SIGNAL(clicked()), &app, SLOT(popup()));
mainWindow->setLayout(layout);
mainWindow->show();
return app.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::popUp()
{
QMessageBox::information(this, "New Box", "Message");
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void popUp();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
私は間違ったことを説明してもらえますか、おそらく、私のコードに何かがありません。
centralWidgetは何ですか? mainwindow.cpp:19:error:不完全な型 'class QLayout'の無効な使用 ui-> centralWidget-> layout() - > addWidget(btn);このウィジェットでエラーが発生しました。 ^ – Viktor
テストが返されました。私の回答を更新してください。 – eyllanesc
@ありがとうございました!なぜこの行でconnect(btn、&QPushButton :: clicked、this、&MainWindow :: popUp)を説明してください。 connect(btn、SIGNAL(clicked())、this、SLOT(popUp()))の代わりに&QPushButton :: clickedと&MainWindow :: popUpを使用する必要があります。 – Viktor