How can I set a QWidget to auto-shrink when some containing QWidgets are set invisible?
-
Hi All,
I have made a Settings dialog, containing a number of widgets, such as Labels, buttons and checkboxes. These are organized in layouts. When I set one of such layouts to invisible, the settingsdialog stays the same size, but all the widgets are spread out.
How can I make sure the settings dialog shrinks when I hide a layout?
here are the steps I've taken:
1)Compile and run the program. Now a window called "Settings" is visible . See 1-Settings-changes-enabled.png
2)Click on the checkbox "Enable changes". Now the majority of the settings are no longer visible, but the window stays the same size See 2-Settings-changes-disabled.png
3)Next I resize the settings window with my mouse, so it's the minimum size. See 3-Settings-minimal size.png
Here's the program that makes the settings dialog:
rs232.h/** Copyright (c) 2020-2020 LUMC. This file is created by Cedric de Wijs * This file is (at your option) licensed as GPL version 2 or any later version * or LGPL version 2 or any later version. See the file LICENSE-GPL.txt and * LICENSE-LGPL.txt for details. This file is distributed in the hope that it * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. In no event shall * the copyright owner or contributors be liable for any damages however caused */ #ifndef RS232_H #define RS232_H #include <QTimer> #include <QtWidgets> #include <QWidget> #include <QSerialPort> #include <QString> #include "ledindicator.h" #include <QObject> #include <QQueue> enum STATES { stateDisabled, stateOpen, statePortOk, stateClose, }; class RS232 : public QWidget { Q_OBJECT public: RS232(QWidget *parent=nullptr); QString stateToString(STATES state); public slots: //settings void slotShowSettings(bool clicked); void slotEnableChangesBoxChanged(void); signals: private: struct Settings { QString name; QString terminators; qint32 baudRate; QString stringBaudRate; }; QIntValidator *m_intValidator = nullptr; Settings m_currentSettings; int myIndex; void showPortInfo(int idx); void fillPortsParameters(void); void fillPortsInfo(void); void gui2Settings(void); void settings2Gui(void); void readSettings(void); void writeSettings(void); QTimer *myTimer; QTimer *myTxTimer; QTimer *myRxTimer; QHBoxLayout *myHlayout; QPushButton *mySettingsBtn; QSerialPort *mySerialPort; QQueue<char> *myRxQueue; int myRxWdCounter=0; STATES myState=stateDisabled; //settings dialog QWidget *settingsWidget = nullptr; QVBoxLayout *settingsVlayout = nullptr; QVBoxLayout *topVlayout = nullptr; QLabel *warningLabel = nullptr; QLabel *warning2Label = nullptr; QCheckBox *enableChangesBox = nullptr; QGridLayout *commstateGLayout = nullptr; QLabel *stateLabel = nullptr; QLabel *myCurrentPortLabel = nullptr; QLabel *myRxdLabel = nullptr; LedIndicator *myRxLED = nullptr; QLabel *myTxdLabel = nullptr; LedIndicator *myTxLED = nullptr; QWidget *portAndParametersWidget = nullptr; QGridLayout *portAndParametersGLayout = nullptr; QLabel *selectPortLabel = nullptr; QComboBox *selectPortComboBox = nullptr; QLabel *descriptionLabel = nullptr; QLabel *manufacturerLabel = nullptr; QLabel *serialNumberLabel = nullptr; QLabel *locationLabel = nullptr; QLabel *vendorIdLabel = nullptr; QLabel *productIdLabel = nullptr; QLabel *selectParametersLabel = nullptr; QLabel *baudrateLabel = nullptr; QComboBox *baudrateComboBox = nullptr; QLabel *endOfLineLabel = nullptr; QLineEdit *endOfLineEdit = nullptr; QCheckBox *carriageReturnBox = nullptr; QCheckBox *newlineBox = nullptr; QHBoxLayout *botLayout = nullptr; QCheckBox *myEnableChkbox; QPushButton *ApplyBtn = nullptr; QPushButton *CancelBtn = nullptr; //end settings dialog //end gui }; #endif // RS232_H
rs232.c
/** Copyright (c) 2020-2020 LUMC. This file is created by Cedric de Wijs * This file is (at your option) licensed as GPL version 2 or any later version * or LGPL version 2 or any later version. See the file LICENSE-GPL.txt and * LICENSE-LGPL.txt for details. This file is distributed in the hope that it * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. In no event shall * the copyright owner or contributors be liable for any damages however caused */ #include "rs232.h" #include "../rs232-config.h" #include <QDebug> #include <QSerialPortInfo> #include <QIntValidator> #include <QtGui> const int LED_SIZE = 10; #define TIME_LED 32 //ms #define TIME_BETWEEN_CHECKS 100 //ms #define TIME_RX_WD 20 //*TIME_BETWEEN_CHECKS #define SETTINGS_GROUP "RS232" #define KEY_ENABLE "enable" #define KEY_COMMPORT "commport" #define KEY_TERMINATORS "terminators" #define KEY_BAUDRATE "baudrate" const QString TXT_NOT_AVAIL = " (NOT AVAILABLE)"; static const char blankString[] = QT_TRANSLATE_NOOP("Rs232Settings", "N/A"); const unsigned char RS232_TERMINATION_CHAR=0x3B; const unsigned char RS232_CR=0x0D; static int objectCount=0; RS232::RS232(QWidget *parent) : QWidget(parent) { mySettingsBtn = new QPushButton; mySettingsBtn->setIcon(QIcon(":/images/wrench.png")); mySettingsBtn->setToolTip("Settings"); myEnableChkbox = new QCheckBox(); myEnableChkbox->setToolTip("Enable"); myHlayout = new QHBoxLayout(); myHlayout->addWidget(mySettingsBtn); //settings dialog settingsVlayout = new QVBoxLayout(); topVlayout = new QVBoxLayout(); QFont font( "Arial", 16, QFont::Bold); warningLabel = new QLabel("Do not change these settings without"); warningLabel->setFont(font); warning2Label = new QLabel("consulting the system administrator"); warning2Label->setFont(font); enableChangesBox = new QCheckBox("Enable Changes"); commstateGLayout = new QGridLayout(); stateLabel = new QLabel(""); myCurrentPortLabel = new QLabel(""); myRxdLabel = new QLabel("Rxd"); myRxLED = new LedIndicator(); myRxLED->setLedSize(LED_SIZE); myRxLED->setOnColor(Qt::green); myRxLED->setToolTip("Rx"); myTxdLabel = new QLabel("Txd"); myTxLED = new LedIndicator(); myTxLED->setLedSize(LED_SIZE); myTxLED->setOnColor(Qt::green); myTxLED->setToolTip("Tx"); commstateGLayout->addWidget(stateLabel,1,1,1,5,Qt::AlignLeft); commstateGLayout->addWidget(myCurrentPortLabel,2,1,Qt::AlignLeft); commstateGLayout->addWidget(myRxdLabel,2,2,Qt::AlignLeft); commstateGLayout->addWidget(myRxLED,2,3,Qt::AlignLeft); commstateGLayout->addWidget(myTxdLabel,2,4,Qt::AlignLeft); commstateGLayout->addWidget(myTxLED,2,5,Qt::AlignLeft); commstateGLayout->setColumnStretch(1,0); commstateGLayout->setColumnStretch(2,0); commstateGLayout->setColumnStretch(3,0); commstateGLayout->setColumnStretch(4,0); topVlayout->addWidget(warningLabel); topVlayout->addWidget(warning2Label); topVlayout->addWidget(enableChangesBox); topVlayout->addLayout(commstateGLayout); portAndParametersGLayout = new QGridLayout(); portAndParametersWidget = new QWidget(); portAndParametersWidget->setLayout(portAndParametersGLayout); selectPortLabel = new QLabel("Select Serial Port"); selectPortComboBox = new QComboBox(); descriptionLabel = new QLabel("Description: "); manufacturerLabel = new QLabel("Manufacturer: "); serialNumberLabel = new QLabel("Serial number: "); locationLabel = new QLabel("Location: "); vendorIdLabel = new QLabel("Vendor Identifier: "); productIdLabel = new QLabel("Product Identifier: "); portAndParametersGLayout->addWidget(selectPortLabel,0,0,Qt::AlignLeft); portAndParametersGLayout->addWidget(selectPortComboBox,1,0,Qt::AlignLeft); portAndParametersGLayout->addWidget(descriptionLabel,2,0,Qt::AlignLeft); portAndParametersGLayout->addWidget(manufacturerLabel,3,0,Qt::AlignLeft); portAndParametersGLayout->addWidget(serialNumberLabel,4,0,Qt::AlignLeft); portAndParametersGLayout->addWidget(locationLabel,5,0,Qt::AlignLeft); portAndParametersGLayout->addWidget(vendorIdLabel,6,0,Qt::AlignLeft); portAndParametersGLayout->addWidget(productIdLabel,7,0,Qt::AlignLeft); selectParametersLabel = new QLabel("Select Parameters"); baudrateLabel = new QLabel("BaudRate"); baudrateComboBox = new QComboBox(); endOfLineLabel = new QLabel("End-Of-Line"); endOfLineEdit = new QLineEdit(); carriageReturnBox = new QCheckBox("\r (carriage return)"); newlineBox = new QCheckBox("\n (newline)"); portAndParametersGLayout->addWidget(selectParametersLabel,0,2,Qt::AlignLeft); portAndParametersGLayout->addWidget(baudrateLabel,1,2,Qt::AlignLeft); portAndParametersGLayout->addWidget(baudrateComboBox,1,3,Qt::AlignLeft); portAndParametersGLayout->addWidget(endOfLineLabel,2,2,Qt::AlignLeft); portAndParametersGLayout->addWidget(endOfLineEdit,2,3,Qt::AlignLeft); portAndParametersGLayout->addWidget(carriageReturnBox,3,2,1,2,Qt::AlignLeft); portAndParametersGLayout->addWidget(newlineBox,4,2,1,2,Qt::AlignLeft); botLayout = new QHBoxLayout(); myEnableChkbox = new QCheckBox("Enable Communication"); ApplyBtn = new QPushButton("Apply"); CancelBtn = new QPushButton("Cancel"); botLayout->addWidget(myEnableChkbox); botLayout->addStretch(); botLayout->addWidget(ApplyBtn); botLayout->addWidget(CancelBtn); settingsVlayout->addLayout(topVlayout); settingsVlayout->addWidget(portAndParametersWidget); settingsVlayout->addLayout(botLayout); settingsWidget = new QWidget(); settingsWidget->setParent(nullptr); settingsWidget->setLayout(settingsVlayout); settingsWidget->setWindowTitle("Settings"); //end settings dialog //settings connect(enableChangesBox,&QCheckBox::toggled,this,&RS232::slotEnableChangesBoxChanged); setLayout(myHlayout); slotShowSettings(false); //debug, auto-show settings } void RS232::slotShowSettings(bool clicked) { Q_UNUSED(clicked); enableChangesBox->setChecked(true); //this does not emit a signal slotEnableChangesBoxChanged(); //so do it manually settingsWidget->show(); } void RS232::slotEnableChangesBoxChanged(void) { if (enableChangesBox->isChecked()) { //unlock here portAndParametersWidget->setVisible(true); myEnableChkbox->setDisabled(false); ApplyBtn->setDisabled(false); } else { portAndParametersWidget->setVisible(false); myEnableChkbox->setDisabled(true); ApplyBtn->setDisabled(true); //howto resise the window back to the smallest size? } }
Cheers,
Cedric