0

I am currently learing Qt, and I am stuck on the problem of using multiple QWidgets and one QMainWindow.

I have setup a project which contains 2 QWidgets and one QMainWindow. This is my idea of using it: design both QWidgets as needed, add them to the mainwindow object, connect the buttons to the correct slots and switch the centerwidget when needed. So I started off with one QMainWindow and then added two QWidgets, including the cpp file, the h file and the ui file. On both QWidgets I added one QPushButton, and called it pushButtonConvert.

Then I went to the cpp file attached to the QMainWindow (mainwindow.cpp) and did the following:

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

Up until this point everything is fine. Now I want to connect the buttons to slots in the mainwindow object, but I can not find the buttons. epochToHuman->pushButtonConvert does not seem to exist, and I can not find any other way to get to the buttons. So am I thinking in a way that is not correct, according to Qt or am I missing something?

Another try at clarifying what I want: I want to use the elements in a QWidget in QMainWindows' cpp file. I want to be able to do things like this:

//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.
Cheiron
  • 3,620
  • 4
  • 32
  • 63
  • Can you post a condensed version of your code or a link to it? I'm having a hard time understanding what you're trying to do. – Mitch Jan 22 '13 at 22:58
  • I added a link to the source and some more text, I hope it is more clear now. – Cheiron Jan 22 '13 at 23:11

1 Answers1

1

You can connect the pushButtonConvert button to MainWindow::convertFromEpochToHuman in the constructor of MainWindow, with:

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

You'll need to make the ui member public first, like you have done for HumanToEpoch.

You should move the declaration of your widgets into MainWindow.h:

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

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

and initialise them like this:

epochToHuman = new EpochToHuman(this);
humanToEpoch = new HumanToEpoch(this);
Mitch
  • 23,716
  • 9
  • 83
  • 122
  • This first gave me the error of ui being private, so in the epochtohuman.h file I move the ui defenition to the public part. After that the following errors came up: C:\Users\Jacko\Documents\GitHub\epochTimeConverter\mainwindow.cpp:18: error: C2027: use of undefined type 'Ui::EpochToHuman' C:\Users\Jacko\Documents\GitHub\epochTimeConverter\mainwindow.cpp:18: error: C2227: left of '->pushButtonConvert' must point to class/struct/union/generic type – Cheiron Jan 22 '13 at 23:49
  • Yep, so you have to include the definition of `Ui::EpochToHuman`, which is in ui_epochtohuman.h. – Mitch Jan 23 '13 at 20:17