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. Simply Code don't show anything
QtWS25 Last Chance

Simply Code don't show anything

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 748 Views
  • 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.
  • J Offline
    J Offline
    Josz
    wrote on last edited by Josz
    #1

    Hello,
    I'm following a book to Qt learn, It tries to explain how to create elements with code; I tried the book codes --below showed-- but don't show anything

    Please, could somebody say me why the next code don work?
    what did I forget?

    Thanks in advance.

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        //Set the top part
        QHBoxLayout *topLayout = new QHBoxLayout(this);
        QComboBox *combo;
        topLayout->addWidget(new QLabel("Impresora"));
        topLayout->addWidget(combo = new QComboBox());
    
        //set the bottom part
        QHBoxLayout *buttonsLayout = new QHBoxLayout(this);
        buttonsLayout->addStretch();
        buttonsLayout->addWidget(new QPushButton("Imprime"));
        buttonsLayout->addWidget(new QPushButton("Cancela"));
    
        QHBoxLayout *groupLayout = new QHBoxLayout(this);
    
    
    }
    
    JonBJ J.HilkJ 2 Replies Last reply
    0
    • J Josz

      Hello,
      I'm following a book to Qt learn, It tries to explain how to create elements with code; I tried the book codes --below showed-- but don't show anything

      Please, could somebody say me why the next code don work?
      what did I forget?

      Thanks in advance.

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          //Set the top part
          QHBoxLayout *topLayout = new QHBoxLayout(this);
          QComboBox *combo;
          topLayout->addWidget(new QLabel("Impresora"));
          topLayout->addWidget(combo = new QComboBox());
      
          //set the bottom part
          QHBoxLayout *buttonsLayout = new QHBoxLayout(this);
          buttonsLayout->addStretch();
          buttonsLayout->addWidget(new QPushButton("Imprime"));
          buttonsLayout->addWidget(new QPushButton("Cancela"));
      
          QHBoxLayout *groupLayout = new QHBoxLayout(this);
      
      
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Josz
      Let's start with: your calling code needs to create the main window, .show() it, and then enter the main event loop. Does it do so? Do you get to see main window but without your widgets, or do you never see anything UI at all?

      J 1 Reply Last reply
      1
      • JonBJ JonB

        @Josz
        Let's start with: your calling code needs to create the main window, .show() it, and then enter the main event loop. Does it do so? Do you get to see main window but without your widgets, or do you never see anything UI at all?

        J Offline
        J Offline
        Josz
        wrote on last edited by
        #3

        @JonB Thanks,

        Good, I use a standard main. I can see the MainWindow but without widgets

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
        
            return a.exec();
        }
        
        1 Reply Last reply
        0
        • J Josz

          Hello,
          I'm following a book to Qt learn, It tries to explain how to create elements with code; I tried the book codes --below showed-- but don't show anything

          Please, could somebody say me why the next code don work?
          what did I forget?

          Thanks in advance.

          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              //Set the top part
              QHBoxLayout *topLayout = new QHBoxLayout(this);
              QComboBox *combo;
              topLayout->addWidget(new QLabel("Impresora"));
              topLayout->addWidget(combo = new QComboBox());
          
              //set the bottom part
              QHBoxLayout *buttonsLayout = new QHBoxLayout(this);
              buttonsLayout->addStretch();
              buttonsLayout->addWidget(new QPushButton("Imprime"));
              buttonsLayout->addWidget(new QPushButton("Cancela"));
          
              QHBoxLayout *groupLayout = new QHBoxLayout(this);
          
          
          }
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @Josz said

          you asign your QMainWindow a layout 3 times.

          Once here

          QHBoxLayout *topLayout = new QHBoxLayout(this);

          and once here

          QHBoxLayout *buttonsLayout = new QHBoxLayout(this);

          and finaly here

          QHBoxLayout *buttonsLayout = new QHBoxLayout(this);

          Each time your previous layout with all its children/asigned widgets get removed. So of course your QMainWindow is empty.

          what I think you meant to do is:

             QHBoxLayout *groupLayout = new QHBoxLayout(this);
          
               QHBoxLayout *topLayout = new QHBoxLayout();
               QComboBox *combo;
               topLayout->addWidget(new QLabel("Impresora"));
               topLayout->addWidget(combo = new QComboBox());
                groupLayout->addLayout(topLayout);
             
               //set the bottom part
               QHBoxLayout *buttonsLayout = new QHBoxLayout(0);
               buttonsLayout->addStretch();
               buttonsLayout->addWidget(new QPushButton("Imprime"));
               buttonsLayout->addWidget(new QPushButton("Cancela"));
               groupLayout->addLayout(buttonsLayout);
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          J 1 Reply Last reply
          3
          • J.HilkJ J.Hilk

            @Josz said

            you asign your QMainWindow a layout 3 times.

            Once here

            QHBoxLayout *topLayout = new QHBoxLayout(this);

            and once here

            QHBoxLayout *buttonsLayout = new QHBoxLayout(this);

            and finaly here

            QHBoxLayout *buttonsLayout = new QHBoxLayout(this);

            Each time your previous layout with all its children/asigned widgets get removed. So of course your QMainWindow is empty.

            what I think you meant to do is:

               QHBoxLayout *groupLayout = new QHBoxLayout(this);
            
                 QHBoxLayout *topLayout = new QHBoxLayout();
                 QComboBox *combo;
                 topLayout->addWidget(new QLabel("Impresora"));
                 topLayout->addWidget(combo = new QComboBox());
                  groupLayout->addLayout(topLayout);
               
                 //set the bottom part
                 QHBoxLayout *buttonsLayout = new QHBoxLayout(0);
                 buttonsLayout->addStretch();
                 buttonsLayout->addWidget(new QPushButton("Imprime"));
                 buttonsLayout->addWidget(new QPushButton("Cancela"));
                 groupLayout->addLayout(buttonsLayout);
            
            J Offline
            J Offline
            Josz
            wrote on last edited by
            #5

            @J.Hilk Thank you very much for the tip.

            unfortunately, the window is still empty with your code :_(

            I don't know why.

            It could be related to what I try to draw on a Ui::MainWindow *ui;?

            J.HilkJ 1 Reply Last reply
            0
            • J Josz

              @J.Hilk Thank you very much for the tip.

              unfortunately, the window is still empty with your code :_(

              I don't know why.

              It could be related to what I try to draw on a Ui::MainWindow *ui;?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @Josz said in Simply Code don't show anything:

              It could be related to what I try to draw on a Ui::MainWindow *ui;?

              oh, I kind of missed that one, try

              ui->centralWidget instead of this as top level widget.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              J 1 Reply Last reply
              4
              • J.HilkJ J.Hilk

                @Josz said in Simply Code don't show anything:

                It could be related to what I try to draw on a Ui::MainWindow *ui;?

                oh, I kind of missed that one, try

                ui->centralWidget instead of this as top level widget.

                J Offline
                J Offline
                Josz
                wrote on last edited by
                #7

                @J.Hilk Thank you very much!! That worked!

                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