0

私は現在Qtを学習していますが、複数のQWidgetsと1つのQMainWindowを使用するという問題に悩まされています。

2つのQWidgetsと1つのQMainWindowを含むプロジェクトをセットアップしました。これは私の考えです。必要に応じて両方のQWidgetsを設計し、それらをメインウィンドウオブジェクトに追加し、ボタンを正しいスロットに接続し、必要に応じてcenterwidgetを切り替えます。そこで、1つのQMainWindowから始めて、cppファイル、hファイル、およびuiファイルを含む2つのQWidgetを追加しました。両方のQWidgetsに1つのQPushButtonを追加し、それをpushButtonConvertと呼びました。

次に、QMainWindow(mainwindow.cpp)に添付されているcppファイルに移動し、次のことを行いました。

EpochToHuman * epochToHuman = new EpochToHuman();
HumanToEpoch * humanToEpoch = new HumanToEpoch();

この時点まで、すべてが正常です。メインウィンドウオブジェクトのスロットにボタンを接続したいのですが、ボタンが見つかりません。epochToHuman-> pushButtonConvertが存在しないようで、ボタンにアクセスする他の方法が見つかりません。それで、Qtによれば、私は正しくない方法で考えているのでしょうか、それとも何かが足りないのでしょうか?

私が欲しいものを明確にする別の試み:QMainWindowsのcppファイルのQWidgetの要素を使用したい。私はこのようなことをしたいです:

//In object MainWindow.cpp
QWidget * a = new QWidget
//Let's say a is a custom widget with a label in it. This label is called Label
a->Label->setText("Hello, World!");
//This gives an error because a does not have a member called Label
//How can I change the text on the label of a?
//And I think if I will be able to change the text of this label, I will also be able to dance around with buttons as needed.
4

1 に答える 1

1

のコンストラクターでpushButtonConvertボタンを次のように接続できます。MainWindow::convertFromEpochToHumanMainWindow

connect(epochToHuman->ui->pushButtonConvert, SIGNAL(clicked(bool)), this, SLOT(convertFromEpochToHuman()));

uiで行ったように、最初にメンバーを公開する必要がありますHumanToEpoch

ウィジェットの宣言を次の場所に移動する必要がありますMainWindow.h

// ...
private:
    Ui::MainWindow *ui;

    EpochToHuman * epochToHuman;
    HumanToEpoch * humanToEpoch;
 // ...

次のように初期化します。

epochToHuman = new EpochToHuman(this);
humanToEpoch = new HumanToEpoch(this);
于 2013-01-22T23:33:23.573 に答える