Important: Please read the Qt Code of Conduct -

Creating a widget and inserting into MainWindow

  • Hi everyone,

    I'm working in the open-source Qt Creator 4.7.0.

    I'm trying to create a large button for touch screens that have a few simple components like a box to indicate Red/Green/Grey for answers, and a QLabel next to it to give the inquiry.

    So far, I created a custom button in code called bigButton. Its base class is QWidget, and its components are another square QWidget and a QLabel. After calling ui->setup and performing a few other things, I try to instantiate one of these bigButtons with my main QWidget as the parent, and I give it some basic geometry to put it somewhere on the main widget, but nothing ever appears. I don't get any errors though.

    I could sure use some guidance on whether my approach is feasible, and if so, I can post some code to show you exactly what I'm doing. If this approach will get me nowhere, I would appreciate some ideas for a new approach.


  • Lifetime Qt Champion

    You are not showing any code so hard to guess at.
    However, be aware that main window have a special widget for center
    called centralWidget() which is what is seen when run.
    So maybe you need to add it to that one and not directly to mainwindow.
    or use widget directly.
    setCentralWidget( new bigButtons(this) )

  • Thanks for the info; I was wondering what centralWidget, generated from the ui, was for.

    I set the button to the central widget of my program, and poof, my button appeared! However, everything else generated from the UI in the previous centralWidget disappeared understandably. So, I changed the parent of my button to ui->centralWidget like so:

    bigButton * test = new bigButton(ui->centralWidget);
        test->setGeometry(QRect(10, 400, 500, 200));

    And at first, the button was being covered by a vertical layout, but after adding it to the vertical layout, it appeared! Thanks for your help!

Log in to reply