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 widget into QGridLayout
Qt 6.11 is out! See what's new in the release blog

Adding widget into QGridLayout

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.3k 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.
  • W Offline
    W Offline
    Waxta
    wrote on last edited by
    #1

    I have problem. I creating widgets, layout and adding them like this:

    QGridLayout * layout = new QGridLayout();
        QLineEdit * firstNumberLabel = new QLineEdit("First number layout", this);
        QLineEdit * secondNumberLabel = new QLineEdit("Second number layout", this);
    
        layout->addWidget(firstNumberLabel, 0, 0);
        layout->addWidget(secondNumberLabel, 1, 0);
    
        this->setLayout(layout);
        this->setWindowTitle("IEEE Calculator");
        this->setFixedSize(1024, 768);
    

    But I can see only one, last created, widget. What is the problem? I added them correctly, propably...

    kshegunovK 1 Reply Last reply
    0
    • W Waxta

      I have problem. I creating widgets, layout and adding them like this:

      QGridLayout * layout = new QGridLayout();
          QLineEdit * firstNumberLabel = new QLineEdit("First number layout", this);
          QLineEdit * secondNumberLabel = new QLineEdit("Second number layout", this);
      
          layout->addWidget(firstNumberLabel, 0, 0);
          layout->addWidget(secondNumberLabel, 1, 0);
      
          this->setLayout(layout);
          this->setWindowTitle("IEEE Calculator");
          this->setFixedSize(1024, 768);
      

      But I can see only one, last created, widget. What is the problem? I added them correctly, propably...

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      What does this refer to? What's the class you're doing this in?

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Waxta
        wrote on last edited by Waxta
        #3

        QMainWindow subclass

        kshegunovK 1 Reply Last reply
        0
        • W Waxta

          QMainWindow subclass

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          Set the layout to the central widget then. Something like this:

          QWidget * viewport = new QWidget(this);
          setCentralWidget(viewport);
          
          QGridLayout * layout = new QGridLayout(viewport);
          viewport->setLayout(layout);
          
          QLineEdit * firstNumberLabel = new QLineEdit("First number layout", this);
          QLineEdit * secondNumberLabel = new QLineEdit("Second number layout", this);
          
          layout->addWidget(firstNumberLabel, 0, 0);
          layout->addWidget(secondNumberLabel, 1, 0);
          
          setWindowTitle("IEEE Calculator");
          

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          3
          • M Offline
            M Offline
            mostefa
            wrote on last edited by
            #5

            Hi @Waxta

            You are trying to add layout to a widget (MainWindow ) which has already a layout,

            Actually according to Qt doc :

            http://doc.qt.io/qt-4.8/qmainwindow.html#qt-main-window-framework

            "QMainWindow has its own layout to which you can add QToolBars, QDockWidgets, a QMenuBar, and a QStatusBar. The layout has a center area that can be occupied by any kind of widget. You can see an image of the layout below...."

            You need to have a central widget (as already suggested by @kshegunov ,and this centralWidget will contain your LineEdits).

            1 Reply Last reply
            1

            • Login

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