Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Hand code GUI completely
Forum Updated to NodeBB v4.3 + New Features

Hand code GUI completely

Scheduled Pinned Locked Moved General and Desktop
29 Posts 9 Posters 18.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goetz
    wrote on last edited by
    #4

    A good way to learn is to use Qt Designer, have the .ui file processed by uic to the ui_xxx.h file and look at the generated code.

    http://www.catb.org/~esr/faqs/smart-questions.html

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #5

      [quote author="Volker" date="1327361774"]A good way to learn is to use Qt Designer, have the .ui file processed by uic to the ui_xxx.h file and look at the generated code.[/quote]

      I disagree here, but perhaps that's my personal view. Uic puts many things in, that are not really needed.

      From my POV, it's easier to use the tutorials or a book, as they describe how to do it and make small, minimal examples with only the really needed stuff.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GroundZero
        wrote on last edited by
        #6

        Thank you for your answers guys, and Gerolf, that is one of the main reasons I want to learn to hand-code it. Only thing is that I am not english and (I think) not fully understand stuff completely.

        I have the book "C++ GUI Programming with Qt 4 (2nd Edition) - The official C++/Qt book" and yes, they explain you how to GUI code a little bit but not in depth.

        They explain a littlebit about QHBoxLayout, QVBoxLayout and that's pritty much it. They explain more about using QDesigner. Then they show you GridBoxLayout and various other things.

        Also, all examples they show are made with QDesigner, and look totally different then when I would write them my self hand-coded. This part confuses me a bit because it all looks so totally different with lots and lots of extra code which I dont need.

        The thing is, I would love to learn how to do this (picture sample):
        !http://img651.imageshack.us/img651/7886/13106021.jpg(sample picture)!

        But when I do something like (a dumb example):

        1. Create a container by using:
          QHBoxLayout = container.

        2. Add two QVBoxLayouts to the container by using:
          QVBoxLayout = leftSide, rightSide.

        First of all, the leftSide and rightSide box wont sit next to each other.
        Besides that, when I add widgets to them they all look 'hussled through each other' lol.
        They dont really align really nice (beautifull).

        Best regards

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #7

          The layout system in Qt is a very very tricky issue!
          I have never used any designer to my codes :)
          What is very important:
          Put all of your widget from top to bottom, and lay these out from bottom to top!

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tobias.hunger
            wrote on last edited by
            #8

            What is wrong about using a designer here? A UI is a very visual thing and directly seeing what I am doing does help me a lot!

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #9

              I would do (schematic):

              @
              QVBoxLayout
              QHBoxLayout
              QGroupBox (personal info)
              QGridLayout
              QLabel - QLineEdit
              QLabel - QLineEdit
              QLabel - QLineEdit
              QLabel - QLineEdit
              QVBoxlayout
              QLabel (a - amount)
              QSlider
              vertical spacer
              QLabel (b - amount)
              QSlider
              vertical spacer
              QLabel (c - amount)
              QSlider
              QGridLayout
              QLabel - QLabel - Spacer
              QLabel - QLabel - Spacer
              QLabel - QLabel - Spacer
              vertical spacer
              QHBoxLayout
              QPushButton
              horizontal spacer
              @

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #10

                [quote author="Tobias Hunger" date="1327417984"]What is wrong about using a designer here? A UI is a very visual thing and directly seeing what I am doing does help me a lot![/quote]

                Yes and no :-)
                I also code all my UIs by hand. This gives me some more options, especially, if you want to use also custom widgets etc.

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tobias.hunger
                  wrote on last edited by
                  #11

                  Gerolf: There are cases where hand-coding makes sense, I agree about that, but in general you are faster and get better results using the designer.

                  This thread is about "how to complete design a GUI by hand". I do challenge that "personal choice" is a valid reason for any professional to hand-craft UIs.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    GroundZero
                    wrote on last edited by
                    #12

                    Thanks for your answer guys, this wil surely help me out a lot.

                    @Gerolf : I will give your suggestion a try to see what I can come up with. Only thing is that I cannot find out how to use spacers and groupboxes. Then again, maybe I am using them in the wrong way lol. I am fairly new to Qt so I am still in the learning process.

                    I will play around with it a bit and reply here with what I came up with :)

                    Thank you all very much so far guys <3

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      briggie
                      wrote on last edited by
                      #13

                      bq. Only thing is that I cannot find out how to use spacers and groupboxes.

                      Spacers are added by going to the target layout and adding:

                      @TargetLayoutName->addstretch()@

                      Where you put it in relation to other widgets matters of course. As for the groupbox, I have not learned that yet.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #14

                        [quote author="GroundZero" date="1327445280"]
                        Thanks for your answer guys, this will surely help me out a lot.

                        Gerolf : I will give your suggestion a try to see what I can come up with. Only thing is that I cannot find out how to use spacers and groupboxes. Then again, maybe I am using them in the wrong way. I am fairly new to Qt so I am still in the learning process.

                        I will play around with it a bit and reply here with what I came up with :)
                        [/quote]

                        @
                        layout->addItem(new QSpacer(...));
                        QGroupBox* pGrp = new QGroupBox(this);
                        QGridLayout* pGrpGrid = new QGridLayout(pGrp);

                        QLabel* pName = new QLabel(pGrp);
                        pGrpGrid->addWidget(0,0,pName);
                        // ...
                        

                        @

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          giesbert
                          wrote on last edited by
                          #15

                          [quote author="Tobias Hunger" date="1327420165"]Gerolf: There are cases where hand-coding makes sense, I agree about that, but in general you are faster and get better results using the designer.

                          This thread is about "how to complete design a GUI by hand". I do challenge that "personal choice" is a valid reason for any professional to hand-craft UIs.[/quote]

                          I would say, it's ok, if you know what you do (and as a professional, you should know :-) ).
                          Why should using a UI tool be better? It might be easier for some things and for some it may not.

                          In MFC, it was not possible to code UI's by hand, as you had no layouters, only coordinates, but with layout classes, it's fairly easy to code the UI by hand.

                          Nokia Certified Qt Specialist.
                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            goetz
                            wrote on last edited by
                            #16

                            Looking at my forms, with tons of widgets, I would drive nuts if I had to hand code them all in C++ :)

                            http://www.catb.org/~esr/faqs/smart-questions.html

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              GroundZero
                              wrote on last edited by
                              #17

                              As said, I am still learning Qt so please go easy on me as I might make dumb mistakes, so my apologies for that if I do so.

                              I wrote this (start) code:
                              @#include <QApplication>
                              #include <QWidget>
                              #include <QGroupBox>
                              #include <QGridLayout>
                              #include <QHBoxLayout>
                              #include <QVBoxLayout>
                              #include <QLabel>
                              #include <QLineEdit>
                              #include <QSlider>
                              #include <QSpinBox>
                              #include <QSpacerItem>
                              #include <QPushButton>

                              int main(int argc, char *argv[])
                              {
                              QApplication app(argc, argv);

                              // create a main widget
                              QWidget *mainAppWindow = new QWidget;
                              mainAppWindow->setWindowTitle("Custom made GUI - Test 0.1");
                              mainAppWindow->setFixedSize(500, 600);
                              
                              // create the widgets
                              QLabel *nameLabel = new QLabel;
                              nameLabel->setText("name");
                              
                              QLineEdit *nameText = new QLineEdit;
                              
                              QSlider *aAmount = new QSlider;
                              
                              aAmount->setRange(0, 100);
                              
                              aAmount->setValue(25);
                              
                              QGroupBox *personalInfoBox = new QGroupBox;
                              personalInfoBox->setTitle("Personal Info");
                              
                              // create the layout for the personalInfoBox
                              QHBoxLayout *pibLayout = new QHBoxLayout;
                              pibLayout->addWidget(nameLabel);
                              pibLayout->addWidget(nameText);
                              pibLayout->addWidget(surnameLabel);
                              pibLayout->addWidget(surnameText);
                              pibLayout->addWidget(ageLabel);
                              pibLayout->addWidget(ageText);
                              pibLayout->addWidget(genderLabel);
                              pibLayout->addWidget(genderText);
                              
                              // create the topleft box and add the group to it
                              QHBoxLayout *topLeftLayout = new QHBoxLayout;
                              topLeftLayout->addLayout(personalInfoBox);
                              
                              // add the layout to the main window
                              mainAppWindow->setLayout(pibLayout);
                              
                              // display the window
                              mainAppWindow->show();
                              
                              return app.exec&#40;&#41;;
                              

                              }@

                              It gives me the following error:
                              no matching function for call to 'QHBoxLayout::addLayout(QGroupBox*&)'

                              I tried looking into the documentation but I cant see what I did wrong. I tried declaring it in two ways:

                              @QGroupBox *personalInfoBox = new QGroupBox;
                              personalInfoBox->setTitle("Personal Info");@

                              and

                              @QGroupBox *personalInfoBox = new QGroupBox("Personal Info");@

                              but both give me the same error. When I use:
                              @QGroupBox *personalInfoBox = new QGroupBox(tr("Personal Info"));@
                              it says that tr cannot be found, but for now dont mind the translate thingy ;)

                              P.S. I shorten the code a bit otherwise the list would be long, but I did declerate all the QLabels and QSliders and other stuff in my code. I have shown you one of each to see how I declerate them, but I remove the others to keep this 'post' a bit smaller ;)

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                giesbert
                                wrote on last edited by
                                #18

                                [quote author="Volker" date="1327494502"]Looking at my forms, with tons of widgets, I would drive nuts if I had to hand code them all in C++ :)[/quote]

                                Spit it up to sub forms? :-)
                                I know there are cases, where it really makes sense, but I try to avoid forms with hundreds of widgets.

                                Nokia Certified Qt Specialist.
                                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                1 Reply Last reply
                                0
                                • G Offline
                                  G Offline
                                  GroundZero
                                  wrote on last edited by
                                  #19

                                  [quote author="Gerolf" date="1327498560"]
                                  [quote author="Volker" date="1327494502"]Looking at my forms, with tons of widgets, I would drive nuts if I had to hand code them all in C++ :)[/quote]

                                  Spit it up to sub forms? :-)
                                  I know there are cases, where it really makes sense, but I try to avoid forms with hundreds of widgets.[/quote]

                                  True, but you can Copy & Paste a lot to though ;)
                                  And I also use the clicking on the numbers on the left side to "fold" code. This way my screen stays pritty clean, and when I need something I can just "unfold" it, edit it or look at it, and fold it back up lol :)

                                  Anyways, I posted my code and question on page 2 (top). I am doing something wrong with the Group option :(

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    giesbert
                                    wrote on last edited by
                                    #20

                                    Hi,

                                    I could boiler plate you, and give you a working version :-)
                                    But it would be complete.

                                    First your bugs:

                                    a group box is a widget --> addWidget not addlayout

                                    pibLayout is the layout of the group box, add it there, not on the main window

                                    Full source for the sollution, only read it if you don't want to learn by building it on your own :-)

                                    @
                                    #include <QApplication>
                                    #include <QWidget>
                                    #include <QGroupBox>
                                    #include <QGridLayout>
                                    #include <QHBoxLayout>
                                    #include <QVBoxLayout>
                                    #include <QLabel>
                                    #include <QLineEdit>
                                    #include <QSlider>
                                    #include <QSpinBox>
                                    #include <QSpacerItem>
                                    #include <QPushButton>

                                    int main(int argc, char *argv[])
                                    {
                                    QApplication app(argc, argv);

                                    // create a main widget
                                    QWidget *mainAppWindow = new QWidget;
                                    mainAppWindow->setWindowTitle("Custom made GUI - Test 0.1");
                                    mainAppWindow->setFixedSize(500, 600);
                                    
                                    // main vertical layout
                                    QVBoxLayout* pMainLayout = new QVBoxLayout(mainAppWindow);
                                    mainAppWindow->setLayout(pMainLayout);
                                    
                                    // ------------------------------------------------------
                                    // first sub layouts
                                    QHBoxLayout* pTopHBox  = new QHBoxLayout;
                                    pMainLayout->addLayout(pTopHBox);
                                    
                                    QGridLayout* pLabelGrid  = new QGridLayout;
                                    pMainLayout->addLayout(pLabelGrid);
                                    
                                    pMainLayout->addItem(new QSpacerItem(0,0,QSizePolicy::Minimum, QSizePolicy::Expanding));
                                    
                                    QHBoxLayout* pBtnHBox  = new QHBoxLayout;
                                    pMainLayout->addLayout(pBtnHBox);
                                    
                                    // ------------------------------------------------------
                                    // create the group box
                                    QGroupBox *personalInfoBox = new QGroupBox(mainAppWindow);
                                    personalInfoBox->setTitle("Personal Info");
                                    
                                    QLabel *nameLabel = new QLabel(QObject::tr("name"), personalInfoBox);
                                    QLineEdit *nameText = new QLineEdit(personalInfoBox);
                                    QLabel *surnameLabel = new QLabel(QObject::tr("surname"), personalInfoBox);
                                    QLineEdit *surnameText = new QLineEdit(personalInfoBox);
                                    QLabel *ageLabel = new QLabel(QObject::tr("age"), personalInfoBox);
                                    QLineEdit *ageText = new QLineEdit(personalInfoBox);
                                    QLabel *genderLabel = new QLabel(QObject::tr("gender"), personalInfoBox);
                                    QLineEdit *genderText = new QLineEdit(personalInfoBox);
                                    
                                    // create the layout for the personalInfoBox
                                    QGridLayout *pibLayout = new QGridLayout;
                                    pibLayout->addWidget(nameLabel, 0, 0);
                                    pibLayout->addWidget(nameText, 0, 1);
                                    pibLayout->addWidget(surnameLabel, 1, 0);
                                    pibLayout->addWidget(surnameText, 1, 1);
                                    pibLayout->addWidget(ageLabel, 2, 0);
                                    pibLayout->addWidget(ageText, 2, 1);
                                    pibLayout->addWidget(genderLabel, 3, 0);
                                    pibLayout->addWidget(genderText, 3, 1);
                                    personalInfoBox->setLayout(pibLayout);
                                    pTopHBox->addWidget(personalInfoBox);
                                    
                                    // ------------------------------------------------------
                                    // create the slider area
                                    QVBoxLayout* pSlidersLayout = new QVBoxLayout;
                                    pTopHBox->addLayout(pSlidersLayout);
                                    QSlider *aAmount = new QSlider(Qt::Horizontal, mainAppWindow);
                                    aAmount->setRange(0, 100);
                                    aAmount->setValue(25);
                                    QSlider *bAmount = new QSlider(Qt::Horizontal, mainAppWindow);
                                    bAmount->setRange(0, 100);
                                    bAmount->setValue(25);
                                    QSlider *cAmount = new QSlider(Qt::Horizontal, mainAppWindow);
                                    cAmount->setRange(0, 100);
                                    cAmount->setValue(25);
                                    
                                    pSlidersLayout->addWidget(new QLabel(QObject::tr("a - amount"), mainAppWindow));
                                    pSlidersLayout->addWidget(aAmount);
                                    pSlidersLayout->addWidget(new QLabel(QObject::tr("b - amount"), mainAppWindow));
                                    pSlidersLayout->addWidget(bAmount);
                                    pSlidersLayout->addWidget(new QLabel(QObject::tr("c - amount"), mainAppWindow));
                                    pSlidersLayout->addWidget(cAmount);
                                    
                                    // ------------------------------------------------------
                                    // create the info area
                                    pLabelGrid->addWidget(new QLabel(QObject::tr("a - statistic display"), mainAppWindow), 0, 0);
                                    pLabelGrid->addWidget(new QLabel(QObject::tr("a - runtime statistic display"), mainAppWindow), 0, 1);
                                    pLabelGrid->addItem(new QSpacerItem(0,0, QSizePolicy::Expanding,QSizePolicy::Minimum), 0, 2);
                                    pLabelGrid->addWidget(new QLabel(QObject::tr("b - statistic display"), mainAppWindow), 1, 0);
                                    pLabelGrid->addWidget(new QLabel(QObject::tr("b - runtime statistic display"), mainAppWindow), 1, 1);
                                    pLabelGrid->addWidget(new QLabel(QObject::tr("c - statistic display"), mainAppWindow), 2, 0);
                                    pLabelGrid->addWidget(new QLabel(QObject::tr("c - runtime statistic display"), mainAppWindow), 2, 1);
                                    
                                    // ------------------------------------------------------
                                    // create the button area
                                    QPushButton* pBtn = new QPushButton(QObject::tr("Push me"), mainAppWindow);
                                    pBtnHBox->addWidget(pBtn);
                                    pBtnHBox->addItem(new QSpacerItem(0,0, QSizePolicy::Expanding,QSizePolicy::Minimum));
                                    
                                    // display the window
                                    mainAppWindow->show();
                                    
                                    return app.exec();
                                    

                                    }
                                    @

                                    Nokia Certified Qt Specialist.
                                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      GroundZero
                                      wrote on last edited by
                                      #21

                                      Gerolf, thank you very much. I will try to fix my code first before copy pasting yours lol, need to learn it anyways haha. Thank you so much once again ;)

                                      P.S. I see you do a LOT of stuff totally different i.e.
                                      @pLabelGrid->addWidget(new QLabel(QObject::tr("c - runtime statistic display"), mainAppWindow), 2, 1);@
                                      @pibLayout->addWidget(nameText, 0, 1);@

                                      you use tr (translate?) and a 2nd attribute. After that you give the QLabel object a 2nd and 3rd attribute (2 and 1).
                                      Never seen that / done that but... ill try to fix my code and try to find out what this all means :)

                                      Also in the 2nd example the INT for a row and so on, thats pritty cool! this will surely help me out a LOT!

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        goetz
                                        wrote on last edited by
                                        #22

                                        [quote author="Gerolf" date="1327498560"]
                                        Spit it up to sub forms? :-)
                                        I know there are cases, where it really makes sense, but I try to avoid forms with hundreds of widgets.[/quote]

                                        It's already subforms :-)
                                        Collecting data isn't always doable with two line edits and a combo box ;-)

                                        http://www.catb.org/~esr/faqs/smart-questions.html

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          giesbert
                                          wrote on last edited by
                                          #23

                                          [quote author="GroundZero" date="1327501166"]P.S. I see you do a LOT of stuff totally different i.e.
                                          @pLabelGrid->addWidget(new QLabel(QObject::tr("c - runtime statistic display"), mainAppWindow), 2, 1);@
                                          @pibLayout->addWidget(nameText, 0, 1);@

                                          you use tr (translate?) and a 2nd attribute. After that you give the QLabel object a 2nd and 3rd attribute (2 and 1).
                                          Never seen that / done that but... ill try to fix my code and try to find out what this all means :)

                                          Also in the 2nd example the INT for a row and so on, thats pritty cool! this will surely help me out a LOT![/quote]

                                          All widgets have parameters in the constructor, one is the parent, which I ALWAYS set. It also works without, then the layout sets them.

                                          the integers in the addWidget is for the grid layout.

                                          Nokia Certified Qt Specialist.
                                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved