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. Synchronizing width of 2 (or more) QGridLayout(s)
Forum Updated to NodeBB v4.3 + New Features

Synchronizing width of 2 (or more) QGridLayout(s)

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k 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
    Wurgl
    wrote on last edited by
    #1

    I have here some window with a QTabWidget and a few tabs. Each tab contains various fields which are positioned with a QGridLayout. Always a pair of QLabel and some input field. Since the text of those QLabels have different length, the first column of those QGridLayouts position those input fields different. Is there some way to get those in sync, so that switching the tab does not cause the inputfields to "jump"?

    Short Example:

    #include <QtCore/QtCore>
    #include <QtGui/QtGui>
    #include <QtWidgets/QtWidgets>
    
    int main(int argc, char *argv[])
    {
      QApplication a(argc, argv);
    
      QTabWidget *tabW = new QTabWidget();
      QWidget *w1 = new QWidget(tabW);
      tabW->addTab(w1, "Tab 1");
      QGridLayout *g1 = new QGridLayout(w1);
      g1->addWidget(new QLabel("Short:", w1), 0, 0);
      g1->addWidget(new QLineEdit(w1), 0, 1);
    
      QWidget *w2 = new QWidget(tabW);
      tabW->addTab(w2, "Tab 2");
      QGridLayout *g2 = new QGridLayout(w2);
      g2->addWidget(new QLabel("Longer Text:", w2), 0, 0);
      g2->addWidget(new QLineEdit(w2), 0, 1);
    
      tabW->show();
      a.exec();
    
      return 0;
    }
    
    kshegunovK 1 Reply Last reply
    0
    • W Wurgl

      I have here some window with a QTabWidget and a few tabs. Each tab contains various fields which are positioned with a QGridLayout. Always a pair of QLabel and some input field. Since the text of those QLabels have different length, the first column of those QGridLayouts position those input fields different. Is there some way to get those in sync, so that switching the tab does not cause the inputfields to "jump"?

      Short Example:

      #include <QtCore/QtCore>
      #include <QtGui/QtGui>
      #include <QtWidgets/QtWidgets>
      
      int main(int argc, char *argv[])
      {
        QApplication a(argc, argv);
      
        QTabWidget *tabW = new QTabWidget();
        QWidget *w1 = new QWidget(tabW);
        tabW->addTab(w1, "Tab 1");
        QGridLayout *g1 = new QGridLayout(w1);
        g1->addWidget(new QLabel("Short:", w1), 0, 0);
        g1->addWidget(new QLineEdit(w1), 0, 1);
      
        QWidget *w2 = new QWidget(tabW);
        tabW->addTab(w2, "Tab 2");
        QGridLayout *g2 = new QGridLayout(w2);
        g2->addWidget(new QLabel("Longer Text:", w2), 0, 0);
        g2->addWidget(new QLineEdit(w2), 0, 1);
      
        tabW->show();
        a.exec();
      
        return 0;
      }
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      Hi @Wurgl,
      Use QFormLayout instead of a grid layout and try playing with the growth policy.

      Kind regards.

      Read and abide by the Qt Code of Conduct

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

        This code seems to work (just the block at the end is added).

        But I am still wondering, is my problem is a special one that is not needed by anyone else or might it be a general one?

        "Jumping" fields may happen with QStackedLayout too?

        However: Solved for me.

        #include <QtCore/QtCore>
        #include <QtGui/QtGui>
        #include <QtWidgets/QtWidgets>
        
        int main(int argc, char *argv[])
        {
          QApplication a(argc, argv);
        
          QTabWidget *tabW = new QTabWidget();
          QWidget *w1 = new QWidget(tabW);
          tabW->addTab(w1, "Tab 1");
          QGridLayout *g1 = new QGridLayout(w1);
          g1->addWidget(new QLabel("Short:", w1), 0, 0);
          g1->addWidget(new QLineEdit(w1), 0, 1);
        
          QWidget *w2 = new QWidget(tabW);
          tabW->addTab(w2, "Tab 2");
          QGridLayout *g2 = new QGridLayout(w2);
          g2->addWidget(new QLabel("Longer Text:", w2), 0, 0);
          g2->addWidget(new QLineEdit(w2), 0, 1);
        
          int maxWidth = 0;
          for(int row = 0; row < g1->rowCount(); ++ row)
            maxWidth = qMax(g1->itemAtPosition(row, 0)->minimumSize().width(), maxWidth);
          for(int row = 0; row < g2->rowCount(); ++ row)
            maxWidth = qMax(g2->itemAtPosition(row, 0)->minimumSize().width(), maxWidth);
          g1->setColumnMinimumWidth(0, maxWidth);
          g2->setColumnMinimumWidth(0, maxWidth);
        
        
          tabW->show();
          a.exec();
        
          return 0;
        }
        
        
        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