Simply Code don't show anything



  • 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);
    
    
    }
    


  • @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?



  • @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();
    }
    


  • @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.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;?



  • @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.Hilk Thank you very much!! That worked!


Log in to reply
 

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