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. tablewidget does not resize with the containing tabwidget in a window?
Forum Updated to NodeBB v4.3 + New Features

tablewidget does not resize with the containing tabwidget in a window?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 972 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.
  • C Offline
    C Offline
    cdwijs
    wrote on last edited by
    #1

    Hi All,
    I'm trying to create a program with a tabWidget where each tab is an instance of a series of nexted V and HboxLayouts. I managed to get a tabwidget that resizes with the main window, but the qtablewidget inside does not resize.
    I've made a small program that shows this in action:

    //mywidget.cpp
    #include "mywidget.h"
    mywidget::mywidget(QWidget *parent) : QWidget(parent)
    {
       myTableWidget = new QTableWidget(this); //does not resize?
    }
    
    //mywidget.h
    #ifndef MYWIDGET_H
    #define MYWIDGET_H
    #include <QtWidgets>
    #include <QWidget>
    class mywidget : public QWidget
    {
    public:
        mywidget(QWidget *parent);
    private:
        QTableWidget *myTableWidget;
    };
    #endif // MYWIDGET_H
    
    //mygui.cpp
    #include "mygui.h"
    #include "mywidget.h"
    myGui::myGui(QWidget *parent) : QWidget(parent)
    {
        QWidget *centralWidget = new QWidget();
        QVBoxLayout *layout = new QVBoxLayout;
        QTabWidget *tabwidget = new QTabWidget;
        tabwidget->addTab(new mywidget(this),"new tab");   
        layout->addWidget(tabwidget);
        centralWidget->setLayout(layout); 
        centralWidget->show();
    }
    
    //mygui.h
    #ifndef MYGUI_H
    #define MYGUI_H
    #include <QtWidgets>
    #include <QWidget>
    class myGui : public QWidget
    {
    public:
        explicit myGui(QWidget *parent=0);
    private:
    };
    #endif // MYGUI_H
    
    //main.cpp
    //#include "mainwindow.h"
    #include <QApplication>
    #include "mygui.h"
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        myGui gui;
        //   MainWindow w;
     //   w.show();
        return a.exec();
    }
    

    What have i missed?
    Cheers,
    Cedric

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      you need to add the table to a layout:

      //mywidget.cpp
      #include "mywidget.h"
      #include <QHBoxLayout>
      mywidget::mywidget(QWidget *parent) : QWidget(parent)
      {
         myTableWidget = new QTableWidget(this);
      QHBoxLayout* mainLay=new QHBoxLayout(this);
      mainLay->addWidget(myTableWidget );
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • C Offline
        C Offline
        cdwijs
        wrote on last edited by
        #3

        Hi Vronin,
        It works, thanks.
        In the real program I'm working on, I have everything in H and V box layouts, but I wrongly placed the topmost layout in a QObject. Then the resizing does not work anymore. Removing the Qobject and all calls to it solved the problem.
        So this does not work:

        QWidget *centralWidget = new QWidget(this);
        QVBoxLayout *centralLayout = new QVBoxLayout(this);
        centralLayout->addWidget(tableWidget);
        centralLayout->addLayout(startLayout);
        centralLayout->addLayout(twiDownLayout);
        centralLayout->addLayout(twiLayout);
        centralLayout->addLayout(cyclLayout);
        centralWidget->setLayout(centralLayout);
        centralWidget->show();
        

        This works:

        QVBoxLayout *centralLayout = new QVBoxLayout(this);
        centralLayout->addWidget(tableWidget);
        centralLayout->addLayout(startLayout);
        centralLayout->addLayout(twiDownLayout);
        centralLayout->addLayout(twiLayout);
        centralLayout->addLayout(cyclLayout);
        
        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