Some clarifications needed on Layout



  • Hi,

    I am currently testing Layouts. I fairly understood how to design Layouts using Layout classes but I run into one specific problem. What if I need to design an interface using more than one window? How to apply Layout classes? I found some people provide codes in which constructors of classes QGridLaout() or QVoxLayout() are provided with this pointer. I tried to understand that reading examples but I couldn't figure it out? For instance I have three codes: main file, header and source file. In source file (the last code) I did not use this in constructors and the application still works. What if I need to have more than one window? I'd be grateful for some words of explanation. Thanks

    @
    // main code
    #include <QWidget>
    #include <QApplication>
    #include <QDesktopWidget>
    #include "myui.h"

    int main(int argc, char* argv[]) {
    QApplication app(argc,argv);
    QDesktopWidget* desktop = new QDesktopWidget();
    const size_t WIDTH = desktop->width();
    const size_t HEIGHT = desktop->height();
    const size_t W = 800;
    const size_t H = 600;
    size_t x = (WIDTH - W)/2;
    size_t y = (HEIGHT - H)/2;

    Myui firstWindow;
    firstWindow.setGeometry(x,y,W,H);
    firstWindow.setWindowTitle("My first UI");
    firstWindow.show();
    
    app.exec();
    

    }
    @

    Below are a header followed by a source file:
    @
    // header
    #ifndef MYUI_H
    #define MYUI_H

    #include <QWidget>

    class Myui : public QWidget {
    public:
    Myui(QWidget* parent = 0);
    };

    #endif // MYUI_H
    @

    @
    // source file
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QPushButton>
    #include <QLabel>
    #include <QTextEdit>
    #include "myui.h"

    Myui::Myui(QWidget *parent)
    : QWidget(parent) {

    QVBoxLayout* firstColumnLayout = new QVBoxLayout();
    firstColumnLayout->setSpacing(10);
    QPushButton* buttons[] = {0,0,0,0,0};
    for(size_t i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++) {
        buttons[i] = new QPushButton(tr("Button %1").arg(i+1));
        firstColumnLayout->addWidget(buttons[i],0,Qt::AlignTop);
    }
    firstColumnLayout->addStretch(1);
    
    QLabel* labels[] = {0,0};
    labels[0] = new QLabel("Label 1");
    labels[1] = new QLabel("Label 2");
    QVBoxLayout* thirdColumnLayout = new QVBoxLayout();
    thirdColumnLayout->addWidget(labels[0],1,Qt::AlignBottom);
    thirdColumnLayout->addWidget(labels[1],0,Qt::AlignBottom);
    
    QTextEdit* textEdit = new QTextEdit();
    textEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    
    QHBoxLayout* mainLayout = new QHBoxLayout();
    mainLayout->addLayout(firstColumnLayout);
    mainLayout->addWidget(textEdit);
    mainLayout->addLayout(thirdColumnLayout);
    
    this->setLayout(mainLayout);
    

    }
    @

    I found in internet that some people use this inside constructors of classes such as QVBoxLayout, QGridLayout, and sometimes they don't providing the same code. When should I use this in constructors?



  • [quote author="Zebbik" date="1343781162"]Hi,
    What if I need to design an interface using more than one window? [/quote]

    Hi, What do you mean by "an interface using more than one window". However, I don't think it has anything to do with QLayout.

    [quote author="Zebbik" date="1343781162"]Hi,
    I found in internet that some people use this inside constructors of classes such as QVBoxLayout, QGridLayout, and sometimes they don’t providing the same code. When should I use this in constructors? [/quote]

    Nothing special.

    If you need to write,
    @
    Widget::Widget()
    {
    QLayout * box = new QHBoxLayout;

    this->setLayout(box);
    }
    @

    You can write

    @
    Widget::Widget()
    {
    QLayout * box = new QHBoxLayout(this);
    }
    @

    BTW, a C++ book will be useful for you.



  • [quote author="1+1=2" date="1343792901"]
    Hi, What do you mean by "an interface using more than one window". However, I don't think it has anything to do with QLayout.[/quote]

    Hi 1+1=2,
    Well, you cut out a phrase from a sentence. Doing this hardly makes sense. What I mean by what you have actually quoted is an application that uses an interface in general "using" more than one window. Let's say I make a database and need an extra window with a form; I want someone to change colour and need an extra window; I need to design a window to change options in my application; just to list a few. What do you suggest if QLayout has nothing to do with it.

    [quote author="1+1=2" date="1343792901"]
    Nothing special.

    If you need to write,
    @
    Widget::Widget()
    {
    QLayout * box = new QHBoxLayout;

    this->setLayout(box);
    }
    @

    You can write

    @
    Widget::Widget()
    {
    QLayout * box = new QHBoxLayout(this);
    }
    @

    BTW, a C++ book will be useful for you.[/quote]
    You know, it still does not explain why these two codes work. What if I design complex layouts where I put one layout into another? Please could you say something more than just "nothing special"? I am really learning here :/. Combing back to point, I still do not understand it and this was basically my question. What exactly I need to look for in my C++ book? My question is about Qt. Considering almost everything, and C++ is not an exception, books explain a lot but not everything. The reason I came here was I did not find the answer and to be perfectly honest with you I still do not see it. You did show two alternative working codes but really did not explain the difference between them. I would be really grateful if you help with that.

    Thanks



  • You should definitely invest some time in learning program structure, not just how to use a program language but a conceptual picture of the data structure you are developing.

    As of why those two codes work - the first uses a default constructor, so you have to set the layout manually, the second one uses a constructor that has a parameter, which s a pointer to the parent object, and the layout is set in the body of its own constructor.

    1 - create layout with default constructor and set layout manually
    2 - create layout with another constructor that sets the layout automatically

    You can put layouts inside layouts inside layouts, there is no restruction.

    As for those "Extra windows" - those will not be layouts but widgets (which can be laid inside of layouts). You can create widgets in floating windows, remove and replace different widgets based on your selection for example, you can use stacked widgets - those are built in functionalities, once you get better at programming you will realize you can do your own as well.

    http://qt-project.org/doc/qt-5.0/mainwindows-mdi.html



  • Hmm, maybe you mean a QDialog to change the settings?
    A Dialog is more appropriate to change settings in stead of making a new window etc etc.



  • Hi Utcenter,

    Thanks for the answer.

    My program is going to be based on a main window with QPushButtons and Edit field where I am going to put text-like results to or perhaps even give some graphs but this is really rather exotic feature at the moment. QPushButtons would correspond to: giving parameters, making calculations based on the parameters, and settings some device-like internal(ish) data. Each button except "Calculation" should open a separate window.

    I am sorry but I don't know what you mean by "learning program structure". I assume you mean the learning Qt program structure. As for my program structure, I have done it in ANSI C++ and already used a command-line program wrapper in C# to get it worked without much changes. The problem is I need to develop a proper GUI for Win, Linux, and Mac OS X. I thought Qt would be the best choice and so far I am still for it.

    As for learning Qt, It is difficult to start and I am not the first person who have already found the website missing documents explaining basics; also, those who have just started feel lost. There is indeed a massive number of references regarding particular classes, examples, and tutorials but all of them assume some knowledge about Qt. They are difficult to read for beginners. I am sure if I understood Qt basics then I would be able to read them but at the moment I only can rely on examples / tutorials and have hope I can figure out enough from them. Over then that, the learning itself is a transition process that requires time. I have just started and even if something is dead easy and perfectly clear for one person is unfortunately not that understandable for someone else.

    BTW thanks for your answers guys :)



  • The feeling of being lost is not because there is a lack of learning materials, but because you are new. Trust me, documentation is plentiful and detailed, there are tons of learning materials, there are at least 5 excellent books on Qt, there are video tutorials on youtube and whatnot.

    Just like with everything else, learning a framework takes time, even if you are already proficient in another language/framework. It will take you at least 4-5 months before things really start clearing out.

    I recommend getting the "Foundations of Qt Development book" and going through it, it will really speed things up.

    Another great and FREE learning resource that will help you out with BOTH Qt and C++ "is this online book, hosted by ICS!":http://www.ics.com/designpatterns/ Requires registration.



  • Hi utcenter,

    I am going to get both ebooks. Thanks for suggested titles.

    Best regards.


Log in to reply
 

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