QPushButton changes fields on other widgets in the same layout

I have a large layout that contains widgets and layouts of the following structure:

QVBoxLayout QTableView QPushButton 

I set the margins, margins, and spacing on the layout to 0. The way this applies to Mac OS X, the button does not fill all its space. Instead, there is a gasket around it. Magically, the layout seems to be aware of this and ensures that the look of the table is as wide as the wide button:

Magic padding caused by QPushButton

When I remove the button, the table view reverts to full width:

No magic padding here

How can I change it so that the button as well as the table view becomes as wide as the layout?

I played with indentation and style fields, and although I can use them to make the button wider, an extra padding around the tree image remains. The only solution I have so far is to wrap the button in another widget and then set its margin and indentation to 0 through the stylesheet, but then I lose the rounded look.

+4
source share
4 answers

you need to set the layout limits to 0, not to the QPushButton fields.

Take a look. This is a design look and feel:

enter image description here

These are the properties of the QWidget layout:

enter image description here

And this is the final widget:

enter image description here

+1
source

After several experiments, I found that it works as expected with QToolButton instead of QPushButton. This is not an explanation, but a solution.

0
source

Hm. interesting. The following code is designed to test your problem, but could not find. it just works as it is. (using Qt 4.8.2 on Mac OSX). The code itself contains. just put it as main.cpp to create the application and click the show and hide button for testing.

 #include <QtGui> class FramedWidget : public QFrame { public: FramedWidget(QWidget* inner, QWidget* parent=0) : QFrame(parent) { setFrameStyle(QFrame::Panel | QFrame::Sunken); QVBoxLayout* lay = new QVBoxLayout(this); lay->setContentsMargins(0,0,0,0); lay->setSpacing(0); lay->addWidget(inner); } }; class LayoutWidget : public QSplitter { public: LayoutWidget(QWidget* parent=0) : QSplitter(parent) { QTableWidget* table = new QTableWidget; m_button = new QPushButton("Testing..."); QPushButton* showButton = new QPushButton("Show"); QPushButton* hideButton = new QPushButton("Hide"); connect(showButton, SIGNAL(clicked()), m_button, SLOT(show())); connect(hideButton, SIGNAL(clicked()), m_button, SLOT(hide())); QWidget* tester = new QWidget; QVBoxLayout* testerLay = new QVBoxLayout(tester); testerLay->addWidget(table); testerLay->addWidget(m_button); QWidget* controller = new QWidget; QVBoxLayout* controllerLay = new QVBoxLayout(controller); controllerLay->addWidget(showButton); controllerLay->addWidget(hideButton); controllerLay->addWidget(new QTextEdit); this->addWidget(new FramedWidget(controller)); this->addWidget(new FramedWidget(tester)); } protected: QPushButton* m_button; }; int main(int argc, char** argv) { QApplication app(argc, argv); LayoutWidget* editor = new LayoutWidget; editor->show(); editor->raise(); return app.exec(); } 
0
source

Another workaround that preserves the look of the button is to place the layout in the QFrame widget container and set the negative fill in the stylesheet. The container must be a QFrame or padding will not work.

0
source

Source: https://habr.com/ru/post/1433091/


All Articles