2013-01-22 122 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