Unsolved QComboBox aligment issue
-
I have a QWidget that contains a QHBoxLayout, that layout contains 2 QComboBoxes. On Mac this widget does not correctly align horizontally with another QComboBox in the same QFormLayout. Even if one of the QComboBoxes in the QWidget is hidden. It is only a few pixels out, but it looks ugly.
WriteModeWidget::WriteModeWidget( QWidget* parent ) : QWidget( parent ) { QSizePolicy sp = sizePolicy(); sp.setHorizontalPolicy( QSizePolicy::Preferred ); sp.setHorizontalStretch( 1 ); setSizePolicy( sp ); QHBoxLayout* hLayout = new QHBoxLayout( this ); hLayout->setSpacing( 1 ); hLayout->setContentsMargins( 0, 0, 0, 0 ); m_writeModeCombo = new QComboBox( this ); hLayout->addWidget( m_writeModeCombo ); m_fileSheetComboBox = new QComboBox( this ); hLayout->addWidget( m_fileSheetComboBox ); }
I've tried setting the QWidget margin and (using a stylesheet) padding. But that doesn't fix it. It looks fine on Windows. Any ideas?
-
You have to set the margins of your WriteModeWidget to 0
-
I tried calling:
setContents( 0, 0, 0, 0 );
In the WriteModeWidget constructor. But it didn't seem to make any difference.
-
@AndyBrice said in QComboBox aligment issue:
setContents( 0, 0, 0, 0 );
QWidget has no such function.
Maybe there is another widget involved - we can't see it. Please provide a minimal, compilable example so we can take a better look on it.
-
@Christian-Ehrlicher Sorry, I meant:
QWidget::setContentsMargins( 0, 0, 0, 0 );
-
Here is a simplified version of the code:
#include "mainwidget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWidget w( nullptr ); w.show(); return a.exec(); } #include <QWidget> class MainWidget : public QWidget { Q_OBJECT public: MainWidget( QWidget *parent ); }; #include "mainwidget.h" #include <QFormLayout> #include <QComboBox> class DualComboBox : public QWidget { public: DualComboBox( QWidget* parent, bool showSecondCombo ); }; DualComboBox::DualComboBox( QWidget* parent ) : QWidget( parent ) { QHBoxLayout* hLayout = new QHBoxLayout( this ); hLayout->setSpacing( 1 ); hLayout->setContentsMargins( 0, 0, 0, 0 ); QComboBox* combo1 = new QComboBox( this ); combo1->addItem( "QComboBox" ); hLayout->addWidget( combo1 ); QComboBox* combo2 = new QComboBox( this ); combo2->addItem( "QComboBox" ); hLayout->addWidget( combo2 ); if ( !showSecondCombo ) combo2->hide(); } MainWidget::MainWidget( QWidget *parent ) : QWidget( parent ) { QFormLayout* fLaypout = new QFormLayout( this ); fLaypout->setFieldGrowthPolicy( QFormLayout::ExpandingFieldsGrow ); DualComboBox* dualComboBox1 = new DualComboBox( this, true ); fLaypout->addRow( "DualComboBox1", dualComboBox1 ); DualComboBox* dualComboBox2 = new DualComboBox( this, false ); fLaypout->addRow( "DualComboBox2", dualComboBox2 ); QComboBox* combo = new QComboBox( this ); combo->addItem( "QComboBox" ); fLaypout->addRow( "QComboBox", combo ); }
This is what it looks like on Mac. Notice that the left alignment is not quite right.
-
The code had a little glitch. Did you try it with a QGridLayout?
It does work fine on Linux therefore I would suggest you to create a Qt bug entry (if possible, otherwise I can do it for you). But please try with QGridlayout first to see if it's the layout component.#include <QtWidgets> class DualComboBox : public QWidget { public: DualComboBox( QWidget* parent, bool showSecondCombo ) : QWidget( parent ) { QHBoxLayout* hLayout = new QHBoxLayout( this ); hLayout->setSpacing( 1 ); hLayout->setContentsMargins( 0, 0, 0, 0 ); QComboBox* combo1 = new QComboBox( this ); combo1->addItem( "QComboBox" ); hLayout->addWidget( combo1 ); QComboBox* combo2 = new QComboBox( this ); combo2->addItem( "QComboBox" ); hLayout->addWidget( combo2 ); if ( !showSecondCombo ) combo2->hide(); } }; class MainWidget : public QWidget { public: MainWidget( QWidget *parent ) : QWidget( parent ) { QFormLayout* fLaypout = new QFormLayout( this ); fLaypout->setFieldGrowthPolicy( QFormLayout::ExpandingFieldsGrow ); DualComboBox* dualComboBox1 = new DualComboBox( this, true ); fLaypout->addRow( "DualComboBox1", dualComboBox1 ); DualComboBox* dualComboBox2 = new DualComboBox( this, false ); fLaypout->addRow( "DualComboBox2", dualComboBox2 ); QComboBox* combo = new QComboBox( this ); combo->addItem( "QComboBox" ); fLaypout->addRow( "QComboBox", combo ); } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWidget w( nullptr ); w.show(); return a.exec(); }
-
I've tried in MacOS.
QComboBox does always have some margins even you set all of the layout's content margins to 0.
I didn't use the OP's code, just made a similar ui by the designer, in both QGridLayout and QFormLayout. -
@Christian-Ehrlicher said in QComboBox aligment issue:
The code had a little glitch.
It was 4 different files, rather than 1 file.
Did you try it with a QGridLayout?
Yes. Same issue.
It does work fine on Linux
It is fine on Windows as well.
therefore I would suggest you to create a Qt bug entry
I will. Thx.