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?
Related
I'm trying to use QGraphicsView with QOpenGLWidget in a MainWindow:
Exactly:
My QMainWindow contains a few layouts and widget. Also a QGraphicsView (class graphicsView inherits from QGraphicsView).
In a "second" step i create a QGraphicsScene and add a openglwidget (class OpenGLControl inherits from QOpenGLWidget). In OpenGLControl I only set the backround for testing my application (glClearColor ...).
Now my request, when I change the size of mainWindow, i want to change the size of QGraphicsView, QGraphicsScene and QOpenGLWidget, too. I try a resizeEvent in graphicsView, but it didn't work.
Implementation of mainwindow (.cpp):
#include "mainwindow.h"
#include "openglcontrol.h"
#include "graphicsview.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createLayout();
createScene();
}
void MainWindow::createLayout()
{
view = new graphicsView;
kosGrid = new QGridLayout;
xKoord = new QLineEdit;
yKoord = new QLineEdit;
zKoord = new QLineEdit;
drawButton = new QPushButton("Draw Point");
clearButton = new QPushButton("Clear");
KoordLabel = new QLabel("Koordinaten");
xLabel = new QLabel("X:");
yLabel = new QLabel("Y: ");
zLabel = new QLabel("Z: ");
controlBox = new QVBoxLayout;
controlBox->addStretch(1);
controlBox->addWidget(KoordLabel);
xhBox = new QHBoxLayout;
xhBox->addWidget(xLabel);
xhBox->addWidget(xKoord);
controlBox->addLayout(xhBox);
yhBox = new QHBoxLayout;
yhBox->addWidget(yLabel);
yhBox->addWidget(yKoord);
controlBox->addLayout(yhBox);
zhBox = new QHBoxLayout;
zhBox->addWidget(zLabel);
zhBox->addWidget(zKoord);
controlBox->addLayout(zhBox);
controlBox->addWidget(drawButton);
controlBox->addWidget(clearButton);
xKoord->setFixedWidth(50);
yKoord->setFixedWidth(50);
zKoord->setFixedWidth(50);
drawButton->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
xKoord->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
yKoord->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
zKoord->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
hBox = new QHBoxLayout;
hBox->addLayout(controlBox);
hBox->addWidget(view);
mainLayout = new QWidget;
mainLayout->setLayout(hBox);
setCentralWidget(mainLayout);
}
void MainWindow::createScene()
{
scene = new QGraphicsScene(this);
qsurface = new QSurfaceFormat;
qsurface->setRenderableType(QSurfaceFormat::OpenGL);
qsurface->setProfile(QSurfaceFormat::CoreProfile);
qsurface->setVersion(3,3);
oglwidget = new OpenGLControl;
oglwidget->setFormat(*qsurface);
oglwidget->
//oglwidget->resize(10,10);
scene->addWidget(oglwidget);
view->setScene(scene);
}
and my attempt for the resizeEvent:
void graphicsView::resizeEvent(QResizeEvent *event)
{
if(scene())
scene()->setSceneRect(QRect(QPoint(0,0), event->size()));
QGraphicsView::resizeEvent(event);
}
at last a screenshot of the (resized) mainWindow (the QOpenGLWidget (blue background) is smaller than the QGraphicsView)
mainWindow with QOpenGLWidget
Is there a SIGNAL I can use ? Or can anybody help me on another way ?
Thank You
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());
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).
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.
I have a QGridLayout with a QSplitter on it. In that QSplitter I have two elements with a splitter that lets me move the splitter from left to right. Fine, there it's fine. But then I want to add another splitter but that moves up to down. (I'll explain with an image.)
So it's mostly having 2 splitters, one that moves left-to-right and other that moves up-to-down.
I hope you understand.
QGridLayout *layout = new QGridLayout(this);
QSplitter *splitter = new QSplitter();
text1 = new QPlainTextEdit();
text2 = new QPlainTextEdit();
splitter->addWidget(text1);
splitter->addWidget(text2);
text1->resize(800, this->height());
layout->addWidget(splitter, 1, 0);
browser = new QTextBrowser();
browser->resize(1, 1);
layout->addWidget(browser, 2, 0);
setLayout(layout);
Here i add only 1 splitter, since i don't know how to do the 2nd one.
You should be able to adapt this for your needs easily. The idea is to create a container for the first two elements, then connect the container with the 3rd element all via splitters.
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget wnd;
QTextEdit *editor1 = new QTextEdit;
QTextEdit *editor2 = new QTextEdit;
QTextEdit *editor3 = new QTextEdit;
QSplitter *split1 = new QSplitter;
QSplitter *split2 = new QSplitter;
QVBoxLayout *layout = new QVBoxLayout;
QWidget *container = new QWidget;
QVBoxLayout *container_layout = new QVBoxLayout;
split1->addWidget(editor1);
split1->addWidget(editor2);
container_layout->addWidget(split1);
container->setLayout(container_layout);
split2->setOrientation(Qt::Vertical);
split2->addWidget(container);
split2->addWidget(editor3);
layout->addWidget(split2);
wnd.setLayout(layout);
wnd.show();
return app.exec();
}