trqa
trqa

Reputation: 25

Qt5 ui, multiple windows: how can I access the Ui objects in Window 2 from Window 1

I know this is very clunky and I'm probably doing a lot of wrong things but so far everything I saw on the net gives back the same errors: invalid use of non-static data member ui.

So in the MainWindow, I have a comboBox named hometeam, and I want to display the currentText on a Qlabel named label which is on another Form Class called Dialog

I figured they're both private members so I added friend class MainWindow and friend class dialog in the respective headers (I know this is pretty wrong but it's the last thing I tried), I included the "ui_mainwindow" and "ui_dialog" in the .cpp files, and here's the bit of code I'm trying:

   ui->label->setText(MainWindow::ui->hometeam->currentTex());

Keep in mind that I don't want a QDialog, the second window will do a lot more than a display, I just want to access the objects from a different window. Slots and signals give the same error.

Thanks !

Upvotes: 0

Views: 159

Answers (1)

p-a-o-l-o
p-a-o-l-o

Reputation: 10047

I think the proper way to do that, is to add a function to your MainWindow class:

QString hometeamText() const
{
    return ui->hometeam->currentTex();
}

This way you can access the information you need without violating encapsulation rules, but you need an instance of MainWindow to do it, and sure must keep a pointer to it in your Dialog class:

class Dialog
{
private:
    MainWindow * mainwindow;
public:
    void setMainWindow(MainWindow * w) { mainWindow = w; }

then somewhere (e.g. in main) you can do something like:

MainWindow mainwindow;
Dialog dialog;
dialog.setMainWindow(&mainWindow);

and from inside your Dialog class, wherever you need it:

ui->label->setText(window->hometeamText());

Upvotes: 1

Related Questions