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. QTableView not expanding/reducing when QMainWindow is resized
Qt 6.11 is out! See what's new in the release blog

QTableView not expanding/reducing when QMainWindow is resized

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

    I have a QMainWindow.

    Inside the QMainWindow, I have a central widget.

    For the central widget, I have a QVBoxLayout.

    To this QVBoxLayout, I've added 3 widgets. One is a widget with some labels, which has a QGridLayout. The other is a horizontal line.

    The third is a QTableView. The problem is that when I try and expand the QMainWwindow, despite whatever resize policy I set ( or not), the TableView does not expand, and it leaves an ungainly blank area in the rest of the expanded QMainWindow.

    Could anyone tell me how I can make the table expand/resize when the Window is resized.

    Here is the relevant code

    @
    statInfoWidget = new Static_Info(TagN);
    QWidget *widget = new QWidget;
    setCentralWidget(widget);
    QFrame *hor_line = new QFrame();
    hor_line->setFrameShape( QFrame::HLine );
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setMargin(2);

    layout->addWidget(statInfoWidget);
    layout->addWidget( hor_line );
    Table = new QTableView(this);
    temp = Table;
    t = new TableLayout(statInfoWidget);
    Table->setModel(t);
    Table->verticalHeader()->hide();
    Table->horizontalHeader()->hide();
    Table->setShowGrid(false);
    Table->setContextMenuPolicy(Qt::CustomContextMenu);
    //Table->setColumnWidth(2,290);
    // Table->setColumnWidth(0,25);
    // if(num_version == 1)
    // Table->setColumnWidth(1,0);
    // else
    // Table->setColumnWidth(1,43);
    // Table->setColumnWidth(3,210);
    // Table->setColumnWidth(4,170);
    // Table->setColumnWidth(5,50);
    statInfoWidget->setStyleSheet("background: rgb(153,185,193);color:rgb(0,0,0); font-family:Tahoma;font-size:19px; border: 0px outset rgb(255,255,255);gridline-color: #669933;"
    "selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #486909, stop: 1 white);");
    Table->setStyleSheet("background: rgb(153,185,193);color:rgb(0,0,0); font-family:Tahoma;font-size:15px; font-weight:bold; border: 0px outset rgb(255,255,255);gridline-color: #669933;"
    "selection-background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 transparent, stop: 1 white);");
    // layout->addWidget(button1);
    QSizePolicy policy = Table->sizePolicy();
    policy.setVerticalStretch(1);
    policy.setHorizontalStretch(1);
    Table->setSizePolicy(policy);
    layout->addWidget(Table);
    widget->setLayout(layout);
    @

    Kindly advise.Thanks.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      starbearer
      wrote on last edited by
      #2

      Any help, please?

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qxoz
        wrote on last edited by
        #3

        Try set horizontal and vertical policy:
        @policy.setHorizontalPolicy(QSizePolicy::Expanding);
        policy.setVerticalPolicy(QSizePolicy::Expanding);@

        1 Reply Last reply
        0
        • S Offline
          S Offline
          starbearer
          wrote on last edited by
          #4

          I've tired both for the Table, both "expanding" as well as "MinimumExpanding".

          Neither seems to work.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on last edited by
            #5

            What is Static_Info,TableLayout? Can you post part of code as project somewhere if it is small?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              starbearer
              wrote on last edited by
              #6

              [quote author="qxoz" date="1362984243"]What is Static_Info,TableLayout? Can you post part of code as project somewhere if it is small?[/quote]

              Static_Info is a simple QWidget, which has a few QLabels as content, and is set in a QGridLayout.

              @
              class Static_Info : public QWidget
              @

              TableLayout is a class derived from QAbstractTableModel.

              @
              class TableLayout : public QAbstractTableModel
              @

              1 Reply Last reply
              0
              • D Offline
                D Offline
                devfeel
                wrote on last edited by
                #7

                I thing using frames and inserting ur widgets in frames and setting policy to frame will resolve.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  starbearer
                  wrote on last edited by
                  #8

                  [quote author="devfeel" date="1362986724"]I thing using frames and inserting ur widgets in frames and setting policy to frame will resolve.[/quote]

                  Could you develop your point, please?

                  And the problem is only with QTableView. The other widgets which I've added to QVBoxLayout of the central widget, they expand fine.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    starbearer
                    wrote on last edited by
                    #9

                    Kindly advise.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      vittalonline
                      wrote on last edited by
                      #10

                      Hi .. starbearer try this code its working fine in my code
                      @
                      QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
                      sizePolicy.setHorizontalStretch(0);
                      sizePolicy.setVerticalStretch(0);
                      sizePolicy.setHeightForWidth(Table->sizePolicy().hasHeightForWidth());
                      Table->setSizePolicy(sizePolicy);
                      @

                      Edit: code goes between @ tags, please; Andre

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        starbearer
                        wrote on last edited by
                        #11

                        Table gets expanded, but unfortunately, it overrides any size which I may have used for my columns.

                        I've set a fixed size for my columns( It's commented out in the code above)....but I need that size.

                        If I use the above method, then all the fixed size for columns gets overriden, and all columns get an equal size.

                        Any way, I can have the columns of a size ( to show the contents in full), and manage to resize the table as well?

                        I tried this...
                        @
                        Table->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
                        @

                        But this had the same effect.

                        Kindly advise...thanks.

                        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