Qtでプログラムを作成したいのですが、2つのボタンのいずれかを押すと、変更したボタンに応じてQLabelのテキストが変わります。スクリプトの実行時にランタイムエラーが発生します。このプログラム用に「カスタム」ウィンドウクラスを作成しました。
これはヘッダーファイルです:
#ifndef MW_H
#define MW_H
#include <QString>
#include <QPushButton>
#include <QLabel>
#include <QGridLayout>
#include <QDialog>
class MW: public QDialog
{
Q_OBJECT
private:
QPushButton* one;
QPushButton* two;
QLabel* three;
QGridLayout* mainL;
public:
MW();
private slots:
void click_1();
void click_2();
};
#endif // MW_H
これは、ヘッダーの.cppです。
#include "MW.h"
MW :: MW()
{
//create needed variables
QGridLayout* mainL = new QGridLayout;
QPushButton* one = new QPushButton("Set1");
QPushButton* two = new QPushButton("Set2");
QLabel* three = new QLabel("This text will be changed");
//connect signals and slots
connect(one, SIGNAL(clicked()), this, SLOT(click_1()));
connect(two, SIGNAL(clicked()), this, SLOT(click_2()));
// create layout
mainL->addWidget(one, 1, 0);
mainL->addWidget(two, 1, 1);
mainL->addWidget(three, 0, 1);
setLayout(mainL);
}
void MW :: click_1()
{
three->setText("One Clicked me!");
}
void MW :: click_2()
{
three->setText("Two Clicked me!");
}
そして最後に、これが主な機能です。
#include <QApplication>
#include "MW.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MW w;
w.setAttribute(Qt::WA_QuitOnClose);
w.show();
return a.exec();
}
これは私が行っている3番目かそこらの小さな学習プログラムであり、私は同じ問題で立ち往生しています。少し面倒になり始めています。どんな助けでもありがたいです。