Results 1 to 2 of 2

Thread: Proper way to manually set the layout of a window

  1. #1
    Join Date
    Sep 2015
    Location
    Lviv, Ukraine
    Posts
    1
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Proper way to manually set the layout of a window

    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?

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. //...
    8. buildLayout();
    9. //...
    10.  
    11. // Now I'd like to use something like this:
    12. // ui->threadingTable->...
    13. // However, it's not the member of ui
    14. }
    15.  
    16. void MainWindow::buildLayout()
    17. {
    18. QWidget *window = new QWidget(this);
    19.  
    20. QTableView *threadingTable = new QTableView(window);
    21. //...
    22.  
    23. QGridLayout *layout = new QGridLayout(window);
    24. layout->addWidget(threadingTable, 0, 0);
    25. //...
    26.  
    27. window->setLayout(layout);
    28. this->setCentralWidget(window);
    29. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Proper way to manually set the layout of a window

    Quote Originally Posted by Oleksandr View Post
    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.

    Quote Originally Posted by Oleksandr View Post
    However, I feel that neither of these is the right way.
    Why? This is how object oriented language are designed to work.

    Quote Originally Posted by Oleksandr View Post
    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Oleksandr (20th September 2015)

Similar Threads

  1. QT program start without proper main window
    By s_eye in forum Qt Programming
    Replies: 6
    Last Post: 2nd July 2014, 14:06
  2. Window Layout - Custom Sizing
    By 2lights in forum Newbie
    Replies: 1
    Last Post: 10th July 2013, 19:03
  3. Help needed for proper Layout mangment
    By sikander243 in forum Newbie
    Replies: 3
    Last Post: 27th February 2013, 12:25
  4. layout's widget does not autofit to window.
    By Niamita in forum Qt Programming
    Replies: 11
    Last Post: 19th October 2011, 14:09
  5. Window Layout issue.
    By bunjee in forum Qt Programming
    Replies: 0
    Last Post: 26th August 2008, 22:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.