Change button size runtime using Qt - qt

I have created a QLineEdit and few QPushButtons in the mainwindow ui and I set there size to (100,100).
If I resize the the window I would like to reize the buttons and a text box at runtime.

you should use layout
For Example look this :
QWidget *window = new QWidget;
QPushButton *button1 = new QPushButton("One");
QPushButton *button2 = new QPushButton("Two");
QPushButton *button3 = new QPushButton("Three");
QPushButton *button4 = new QPushButton("Four");
QPushButton *button5 = new QPushButton("Five");
QGridLayout *layout = new QGridLayout(window);
layout->addWidget(button1, 0, 0);
layout->addWidget(button2, 0, 1);
layout->addWidget(button3, 1, 0, 1, 2);
layout->addWidget(button4, 2, 0);
layout->addWidget(button5, 2, 1);
window->show();

Related

How to move QWidget in QAbstractItemView?

I have a QComboBox, which has many items on it:
combo->addItem("def");
combo->addItem("abc");
Now I would like to add QWidget to one of my item, for example:
QPushButton *button = new QPushButton;
button->setStyleSheet("QPushButton {background:red}");
button->setFixedSize(10,10);
QModelIndex index = combo->model()->index(0,0);
combo->view()->setIndexWidget(index, button);
I set button's size to 10x10 ( of course my QComboBox is bigger ). And I would like to move this button to other place ( picture ).
You can try using layout
QPushButton *button = new QPushButton;
button->setStyleSheet("QPushButton {background:red}");
button->setFixedSize(10,10);
auto widget = new QWidget{this};
auto layout = new QHBoxLayout{widget};
layout->setContentsMargins(0, 0, 0, 0);
layout->addStretch();
layout->addWidget(button);
QModelIndex index = combo->model()->index(0,0);
combo->view()->setIndexWidget(index, widget);

Why QPushButton.resize() don't work in QVBoxLayout?

I'm making a UI, as shown in the figure.
For the four buttons in the middle (ops_area), I used QVBoxLayout to manage them. I used resize() to set the size of the buttons, but the size of the buttons did not change.
...
btn_add_model_to_selectedModels_area_ = new QPushButton(">", this);
btn_add_model_to_selectedModels_area_->resize(40, 30);
btn_add_model_to_selectedModels_area_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
btn_add_all_models_to_selectedModels_area_ = new QPushButton(">>", this);
btn_add_all_models_to_selectedModels_area_->resize(40, 30);
btn_delete_model_from_selectedModels_area_ = new QPushButton("<", this);
btn_delete_model_from_selectedModels_area_->resize(40, 30);
btn_delete_all_models_from_selectedModels_area_ = new QPushButton("<<", this);
btn_delete_all_models_from_selectedModels_area_->resize(40, 30);
...
QHBoxLayout *hLayout = new QHBoxLayout(this);
QHBoxLayout *modelLibrary_load_delete_btn_area_ = new QHBoxLayout(this);
QHBoxLayout *selectedModels_confirm_cancel_btn_area_ = new QHBoxLayout(this);
QVBoxLayout *modelLibrary_area = new QVBoxLayout(this);
QVBoxLayout *ops_area = new QVBoxLayout(this);
...
ops_area->addStretch(0);
ops_area->addWidget(btn_add_model_to_selectedModels_area_);
ops_area->addWidget(btn_add_all_models_to_selectedModels_area_);
ops_area->addWidget(btn_delete_model_from_selectedModels_area_);
ops_area->addWidget(btn_delete_all_models_from_selectedModels_area_);
ops_area->addStretch(0);
What is the correct way to make a fixed size button? How can I fixed it?

How to extract widgets from QHBoxLayout in Qt

I have a TableWidget into which I add Widgets like this:
QLabel *l = new QLabel("TEST");
QWidget *widget = new QWidget();
QHBoxLayout *hbox = new QHBoxLayout();
hbox->addWidget(l);
hbox->setAlignment(Qt::AlignCenter);
hbox->setContentsMargins(0,0,0,0);
widget->setLayout(hbox);
ui->tableWidget->setCellWidget(0, 0, widget);
When a cell gets double clicked I capture the event and would like to figure out what QLabel it is.
But how do I extract it again or do I even have to?
auto widget = ui->tableWidget->cellWidget(ui->tableWidget->currentRow(), ui->tableWidget->currentColumn()); // if mode is SingleSelection
auto hbox = widget->layout();
auto label = qobject_cast<QLabel *>(hbox->itemAt(0)->widget());

Set widget background color

I use QCheckBox in QTableWidgetCell
QWidget *widget = new QWidget();
QCheckBox *checkBox = new QCheckBox();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);
table->setCellWidget(0, 0, widget);
How can I change cell background?
The code:
widget->setStyleSheet("background-color: red");
works fine but you need to set the style for every container widget you add to your table:
So in order to see the change you need the following code:
QWidget *widget = new QWidget();
widget->setStyleSheet("background-color: red");
QCheckBox *checkBox = new QCheckBox();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);
QWidget *widget2 = new QWidget();
widget2->setStyleSheet("background-color: red");
QCheckBox *checkBox2 = new QCheckBox();
QHBoxLayout *layout2 = new QHBoxLayout(widget2);
layout2->addWidget(checkBox2);
layout2->setAlignment(Qt::AlignCenter);
layout2->setContentsMargins(0, 0, 0, 0);
widget2->setLayout(layout);
ui->tableWidget->setCellWidget(0, 0, widget);
ui->tableWidget->setCellWidget(0, 1, widget2);
And the result will be:
You should try this:
checkBox->setStyleSheet("background-color: red;");
If you want to specify it more generally, write the classtype in the CSS to indicate which class in the hierarchy should handle the flag. This could look something like this then:
QWidget { background-color: red; }
If you want to change cell background, not a widget, use setBackground() method:
QCheckBox *checkBox = new QCheckBox("example");
QWidget *widget = new QWidget();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);
ui->tableWidget_2->setCellWidget(0,0,widget);
ui->tableWidget_2->item(0, 0)->setBackground(Qt::red);//this line should be
In this case all your cell will be red (without white lines around checkbox).

Qt widgets not placed correctly

I've subclassed QWidget and defined constructor this way:
LoupingWidget::LoupingWidget(QWidget *parent): QWidget(parent)
{
QGroupBox *topGroupBox = new QGroupBox(this);
QGraphicsView *xRGBPlot = new QGraphicsView(this);
QGraphicsView *yRGBPlot = new QGraphicsView(this);
QGraphicsView *loupe = new QGraphicsView(this);
QSlider *slider = new QSlider(this);
QGridLayout *boxGLayout = new QGridLayout;
boxGLayout->addWidget(xRGBPlot, 0, 0);
boxGLayout->addWidget(slider, 0, 1);
boxGLayout->addWidget(loupe, 1, 0);
boxGLayout->addWidget(yRGBPlot, 1, 1);
topGroupBox->setLayout(boxGLayout);
}
Next, I am trying to add it in a QDialog:
Window::Window(QWidget *parent): QDialog(parent)
{
LoupingWidget *firstLoupindWidget = new LoupingWidget(this);
LoupingWidget *secondLoupindWidget = new LoupingWidget(this);
// QGraphicsView *mainPicture = new QGraphicsView(this);
QGridLayout *gridLayout = new QGridLayout;
// gridLayout->addWidget(mainPicture, 0, 0);
gridLayout->addWidget(firstLoupindWidget, 1, 0);
gridLayout->addWidget(secondLoupindWidget, 1, 1);
setLayout(gridLayout);
}
When this two lines are commented out, two widgets are placed horizontally.
And that's good, but when I uncomment lines with another QGraphicsViews, it fills entire window.
What am I doing wrong?
LoupingWidget doesn't have a layout, so when it's added to another layout, layout can't resize it according to its contents. You need to create another layout (e.g. QGridLayout) in LoupingWidget constructor, add topGroupBox to the layout and set the layout as LoupingWidget's layout.

Resources