Design from book in Code. Don't show anything



  • Hi,
    like I said in a previous post, I'm following a book to learn qt ;
    I'm trying to make the next design in Code: http://img.fenixzone.net/i/aZI6E8F.png

    Therefore, according the book, I should use the code below, for the three red squares

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        setFixedSize(1024,768);
    
        QHBoxLayout *topLayout = new QHBoxLayout(this);
        QComboBox *combo;
        topLayout->addWidget(new QLabel("Printer"));
        topLayout->addWidget(combo = new QComboBox());
    
        //set the bottom part
        QHBoxLayout *buttonsLayout = new QHBoxLayout(this);
        buttonsLayout->addStretch();
        buttonsLayout->addWidget(new QPushButton("Print"));
        buttonsLayout->addWidget(new QPushButton("Cancel"));
    
        //set the central part //ui->centralWidget
        QHBoxLayout *groupLayout = new QHBoxLayout(this);
        QGroupBox *orientationGroup = new QGroupBox();
        groupLayout->addWidget(orientationGroup);
        QGroupBox *colorGroup = new QGroupBox();
        groupLayout->addWidget(colorGroup);
    }
    

    The problem is that the things don't work as expected and the code show nothing, as I described in the previous post: https://forum.qt.io/topic/95914/simply-code-don-t-show-anything.

    Unfortunatelly, the solution [replace *QHBoxLayout topLayout = new QHBoxLayout(this);for *QHBoxLayout groupLayout = new QHBoxLayout(ui->centralWidget);], very kindly contributed, when solution for the thread, don't help to the desired design.

    Please, could somebody help me with de design? (I would like show, as beginning, only the three red squares without sub elements into MainWindow)
    Any help is welcommed. When only links to docs and examples, whith great pleasure.

    Thanks in advance.



  • @Josz
    I don't understand.

    In https://forum.qt.io/topic/95914/simply-code-don-t-show-anything @J-Hilk showed you that you cannot keep creating QLayout(this) on a main window; instead you must start from ui->centralWidget. And you ended up saying "Thank you very much!! That worked!".

    Now you have reverted to code which we know won't work, and said "and the code show nothing". Which we know will be the case. So what is the point in reverting to this code?

    If the proper code does at least work to show something, but then you want to discuss/adjust that layout, would it not be better to start from the code (using ui->centralWidget) which shows everything properly, and then work from there?

    In the case of a main window, everything should descend from centralWidget, not from this.

    As for your actual problem. do you just mean you want to add, say, one QVBoxLayout to the central widget, and then add your sub-widgets ("three red squares") to that, so they are laid out vertically as top, middle, bottom?



  • @JonB Thank John,
    good, I ended with "that worked", because @J-Hilk gave me a solution for the thread, but I could not use it for my desired design, so the thread was solved but not my doubts; I thought it would be better a new thread open.

    Due to my ignorance (and the book... so I see, is not good), thought that [ui->centralWidget] was not the right start point for the design.

    Well, I'm glad I was wrong.

    Then the next Question is, once that I have the first element in the middle.. QHBoxLayout *groupLayout = new QHBoxLayout(ui->centralWidget);

    How can I add the upper and lower squares?

    Thanks in advance.



  • @Josz
    I don't think you want a QHBoxLayout. That is a horizontal layout, so each thing you add onto it will be placed to the right of the previous thing. I think you mean you want a QVBoxLayout, a vertical layout, so each thing you add onto it will be placed beneath the previous thing.

    Start by add one QVBoxLayout. Then add, say, 3 QPushButton widgets onto it. See how they are vertically arranged? Now you can replace each one with whatever you want. You can even replace one with a QHBoxLayout and then add multiple QWidget onto that, so that "row" holds multiple widgets which are themselves arranged left-to-right.



  • @JonB You was right! Thank you!

    Here is the code (if someone else have same issue):

        QVBoxLayout *para3CajasRojasHorizontales = new QVBoxLayout(ui->centralWidget);
        QHBoxLayout *topLayout = new QHBoxLayout(this);
        QComboBox *combo;
        topLayout->addWidget(new QLabel("Printer"));
        topLayout->addWidget(combo = new QComboBox());
        para3CajasRojasHorizontales->addLayout(topLayout);
    
        //set the central part //ui->centralWidget
        QHBoxLayout *groupLayout = new QHBoxLayout(this);
        QGroupBox *orientationGroup = new QGroupBox();
        groupLayout->addWidget(orientationGroup);
        QGroupBox *colorGroup = new QGroupBox();
        groupLayout->addWidget(colorGroup);
        para3CajasRojasHorizontales->addLayout(groupLayout);
    
        //set the bottom part
        QHBoxLayout *buttonsLayout = new QHBoxLayout(this);
        buttonsLayout->addStretch();
        buttonsLayout->addWidget(new QPushButton("Print"));
        buttonsLayout->addWidget(new QPushButton("Cancel"));
        para3CajasRojasHorizontales->addLayout(buttonsLayout);
    


  • @Josz
    I am glad you are happy.

    Because of our discussion about your use of this in your new QHBoxLayout(this);, personally I would not pass this as parent. I would pass no parent, or if you prefer ui->centralWidget, which is what it is going to end up being later on. I think it would at least benefit you to understand why/what is going on here. You might want to read through e.g. https://stackoverflow.com/questions/35927311/qt5-what-is-the-significance-of-a-layouts-parent and the accepted solution there.



  • @JonB Thanks again JohnB


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.