2013-01-22 123 views
0

我目前正在學習Qt,並且我被困在使用多個QWidgets和一個QMainWindow的問題上。將多個QWidets合併爲一個QMainWindow

我設置了一個包含2個QWidgets和一個QMainWindow的項目。這是我使用它的想法:根據需要設計兩個QWidgets,將它們添加到主窗口對象,將按鈕連接到正確的插槽並在需要時切換中心組件。所以我從一個QMainWindow開始,然後添加了兩個QWidgets,包括cpp文件,h文件和ui文件。在兩個QWidgets中,我添加了一個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. 
+0

您可以發佈您的代碼或一個濃縮版,以它的鏈接?我很難理解你想要做什麼。 – Mitch

+0

我添加了一個鏈接來源和一些更多的文字,我希望現在更清楚。 – Cheiron

回答

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); 
+0

這首先給我的ui是私人的錯誤,所以在epochtohuman.h文件中,我將ui defenition移動到公共部分。之後出現以下錯誤: C:\ Users \ Jacko \ Documents \ GitHub \ epochTimeConverter \ mainwindow.cpp:18:錯誤:C2027:使用未定義類型'Ui :: EpochToHuman' C:\ Users \ Jacko \ Documents \ GitHub \ epochTimeConverter \ mainwindow.cpp:18:error:C2227:' - > pushButtonConvert'的左邊必須指向class/struct/union/generic類型 – Cheiron

+0

是的,所以你必須包含'Ui :: EpochToHuman ',它位於ui_epochtohuman.h中。 – Mitch