#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); connect( ui->applyStyle, &QPushButton::clicked, this, &MainWindow::applyStyle ); connect( ui->addBox, &QPushButton::clicked, this, &MainWindow::addBox ); } MainWindow::~MainWindow() { delete ui; } void MainWindow::applyStyle( void ) { // apply style sheet to whole application (gray background and white foreground color) QApplication* app = static_cast< QApplication* >( QApplication::instance() ); app->setStyleSheet( "QWidget { background-color: gray; color: white; }" ); } void MainWindow::addBox( void ) { QHBoxLayout* layout = new QHBoxLayout(); // add a QComboBox to the new row auto box = new QComboBox(); layout->addWidget(box); // add a QPushButton to the new row, which enables editing of the assosiated QComboBox if clicked auto enable = new QPushButton( "Editable"); connect( enable, &QPushButton::clicked, [=]() -> void { box->setEditable( true ); } ); layout->addWidget(enable); // add a QPushButton to the new row, which disables editing of the assosiated QComboBox if clicked auto disable = new QPushButton( "!Editable" ); connect( disable, &QPushButton::clicked, [=]() -> void { box->setEditable( false ); } ); layout->addWidget(disable); // add new row to vertical layout in main window ui->boxLayout->addLayout( layout ); }