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. Adding a widget to a QFrame

Adding a widget to a QFrame

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 33.1k 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.
  • S Offline
    S Offline
    salunkerahul
    wrote on last edited by
    #1

    I have a QMainWindow which has a QFrame(object name: navigationFrame) and I struggling to figure out a way to add a user widget(navigationWidget) onto this frame programmatically.

    Is there a way to do it?

    I tried the following:
    layout->addWidget(navigationWidget);

    Also, what is the recommended practice when adding user defined widgets within a form?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Is there a way to do it?

      If the main window is the standard main window created by the wizard and the navigationForm was added in the designer then you can access it through the ui member of the main window class, e.g.:

      auto layout = new QVBoxLayout();
      layout->addWidget(navigationWidget);
      ui->navigationFrame->setLayout(layout);
      

      Also, what is the recommended practice when adding user defined widgets within a form?

      You place a "dummy" plain widget (or the base class of your custom widget) in the editor and then promote it to your custom class.

      1 Reply Last reply
      2
      • Jimmy CrabJ Offline
        Jimmy CrabJ Offline
        Jimmy Crab
        wrote on last edited by
        #3

        My code:

           //panelCalculate is a QFrame
            QPushButton* btn = new QPushButton(ui->panelCalculate);
            btn->setText("ABC");
        

        It works:
        2022-05-29_09-15-00.png

        Just do it.

        mrjjM 1 Reply Last reply
        0
        • Jimmy CrabJ Jimmy Crab

          My code:

             //panelCalculate is a QFrame
              QPushButton* btn = new QPushButton(ui->panelCalculate);
              btn->setText("ABC");
          

          It works:
          2022-05-29_09-15-00.png

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Jimmy-Crab

          Hi
          Yes if you use the panel as a parent, the buttons become a child widget (and shown inside). So that sort of works.
          However, without a layout, you must manually position them as else they all be shown at pos 0,0 as the first one is.
          And then if you resize your window, they won't follow / change positions so one should really try to use a layout as
          Chris-Kawa shows. :)

          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