PDA

View Full Version : Proper way to manually set the layout of a window



Oleksandr
20th September 2015, 13:00
I need a QMainWindow layout to change depending on the number of cores.
Therefore I set it manually (not using the Design mode).

My question is:
After this layout was created, how can I refer to the widgets it contains?


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

//...
buildLayout();
//...

// Now I'd like to use something like this:
// ui->threadingTable->...
// However, it's not the member of ui
}

void MainWindow::buildLayout()
{
QWidget *window = new QWidget(this);

QTableView *threadingTable = new QTableView(window);
//...

QGridLayout *layout = new QGridLayout(window);
layout->addWidget(threadingTable, 0, 0);
//...

window->setLayout(layout);
this->setCentralWidget(window);
}

I can get the QLayoutItem out of this->centralWidget().
Or I can make all widgets in layout members of MainWindow class and access them directly.

However, I feel that neither of these is the right way.
Is there a way to pass the widgets to ui?

So that I could access them by calling
ui->threadingTable

anda_skoa
20th September 2015, 16:44
Or I can make all widgets in layout members of MainWindow class and access them directly.

That's the way to do it.
Member variables exist for the purpose of having access to data from all methods of an object.



However, I feel that neither of these is the right way.

Why? This is how object oriented language are designed to work.



Is there a way to pass the widgets to ui?

ui is a pointer to an object of a generated class.
You can of course derive from that class, add your additional members to that and then create an instance of it.

Or you use a second container object of a custom class.

Cheers,
_