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



  • 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



  • 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 );
    }
    


  • 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);
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.