Child widgets in a QHBoxLayout are not disabled when the layout is disabled
-
Hi All,
I have a user interface that uses a number of nested V and Hbox layouts, containing various widgets. I would like to lock all the widgets in the user interface. When I disable the HBoxlayout, the widgets inside them are not affected.
I've made the following program to demonstrate this, I expected mainLay->setEnabled(false); to disable both mainLay and myTableWidget, but instead it only disables mainLay.What did I miss?
Cheers,
Cedric//debug output: Debugging starts Defaults: mainLay isEnabled: true myTableWidget isEnabled: true Disable mainLay mainLay isEnabled: false myTableWidget isEnabled: true Disable tableWidget mainLay isEnabled: false myTableWidget isEnabled: false
//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(); }
//mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "mywidget.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
//mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include "mywidget.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->tabWidget->addTab(new mywidget(this),"NewTab"); } MainWindow::~MainWindow() { delete ui; }
//mygui.h #ifndef MYGUI_H #define MYGUI_H #include <QtWidgets> #include <QWidget> class myGui : public QWidget { public: explicit myGui(QWidget *parent=0); private: };
//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(); }
//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
//mywidget.cpp #include "mywidget.h" #include <QHBoxLayout> #include <QDebug> mywidget::mywidget(QWidget *parent) : QWidget(parent) { myTableWidget = new QTableWidget(this); QHBoxLayout* mainLay=new QHBoxLayout(this); mainLay->addWidget(myTableWidget ); qDebug()<<"Defaults:"; qDebug()<<"mainLay isEnabled: "<<mainLay->isEnabled(); qDebug()<<"myTableWidget isEnabled: "<<myTableWidget->isEnabled(); qDebug()<<"Disable mainLay"; mainLay->setEnabled(false); //does not alter tablewidget inside it? qDebug()<<"mainLay isEnabled: "<<mainLay->isEnabled(); qDebug()<<"myTableWidget isEnabled: "<<myTableWidget->isEnabled(); qDebug()<<"Disable tableWidget"; myTableWidget->setEnabled(false); qDebug()<<"mainLay isEnabled: "<<mainLay->isEnabled(); qDebug()<<"myTableWidget isEnabled: "<<myTableWidget->isEnabled(); }
//my versions: Windows 7 enterprise SP1 64 bit Qt Creator 3.6.0 Based on Qt 5.5.1 (MSVC 2013, 32 bit) C:/Qt/Qt5.5/Tools/mingw492_32/bin/mingw32-make -f Makefile.Debug
-
Hi! QWidgets are organized in trees. If you disable a widget that's up in that hierarchy then it will also disable all its children. But the QLayout classes aren't widgets, they are not part of that hierarchy. To disable all widgets that are managed by a QLayout you need to disable their common parent widget. If the common parent also has children that you don't want to disable then you need to introduce some "helper widget" as common parent. Disabling a QLayout only tells it not to manage the widgets, that were assigned to it, anymore.