Creating a widget...
-
I am trying to create a widget that will contain a QPushButton and a QLineEdit, the push button will contain just ... and will invoke the Qt file selection dialog, the QLineEdit will display the selection.
So far I have:
QWidget* pFSContr = new QWidget(); QGridLayout* pgrdFSLayout = new QGridLayout(); QPushButton* pbtnBrowse = new QPushButton(); QLineEdit* plnedFS = new QLineEdit(); pFSContr->setLayout(pgrdFSLayout); pFSContr->setMininumSize(0, 0); QRect rctGeom(pbtnBrowse->geometry()); rctGeom.setWidth(24); rctGeom.setHeight(24); pbtnBrowse->setGeometry(rctGeom); pgrdFSLayout->addWidget(pbtnBrowse, 0, 0, 1, 1, Qt::AlignLeft); pgrdFSLayout->addWidget(plnedFS, 0, 1, 1, 1, Qt::AlignLeft);Is there a layout or setting that will allow me to set the size of the contained widgets as presently any size's set are ignored ?
-
Setting a geometry on a widget that is governed by a layout has no effect, as it's the layout's actual job to set that geometry. You can, however, manage the minimum and maximum size of the widget that the layout will respect. To hardcode a constant size of a widget you can set its minimum and maximum size to the same value. There's also a shortcut function that lets you do both in one go: setFixedSize.
Keep in mind though, that fixing a widget's size is not a good idea, especially if it contains any sort of text (even if it's just
...). Different users will have different screens with different DPI and scaling settings and whatever looks good on your monitor might become too small or have the text not fit inside the widget on another screen with different scaling. A far more robust approach is to manage size policies and stretch factors, which deal in a more abstract or proportion based units that will scale properly on different displays.Also - passing the alignment parameter to
addWidgetis probably not what you actually mean here. This parameter is for cases when all widgets in layout have maximum sizes that sum to less than the parent widget's size. In that case the alignment is used to position such widget within layout grid's cell. In your case you've got an expanding line edit, which means the widgets will occupy entire available space in the layout so setting alignment has no use. I'd leave that parameter at its default value.