PDA

View Full Version : QWidget promotion with child widgets



Phlucious
17th February 2012, 23:50
I would like to create a simple file retrieval widget that has within it a QLineEdit and a QPushButton within a QHBoxLayout. When a user clicks on the QPushButton, it opens a QFileDialog to find a file whose path gets placed into the QLineEdit.

Normally, I would just sub-class QWidget as MyFileLineEdit and give it the QLineEdit and QPushButton as children, but functionally it's basically a QLineEdit with extra features and I would like it to interface like one (particularly with Qt Designer via promotion).

Is there a way to do this without manually connecting all of QLineEdit's signals and slots in MyFileLineEdit? I have tried to inherit directly from QLineEdit, but I can't figure out how to insert the QPushButton next to it.

wysota
18th February 2012, 08:44
Yes. Subclass QLineEdit, use QWidget::setContentMargins() to "push" the right edge of the widget towards left to make room for the button, then create an instance of QToolButton (it's much better than push button here), make it a child of QLineEdit and position it in the space available.

Or just use QwwFileChooser (http://www.wysota.eu.org/wwwidgets/doc/html/qwwfilechooser.html) or QwwButtonLineEdit (http://www.wysota.eu.org/wwwidgets/doc/html/qwwbuttonlineedit.html).

Phlucious
21st February 2012, 18:02
That's exactly what I was looking for! Interestingly, those two wwWidgets you linked are pretty much the immediate applications that I had in mind.

What are the advantages of using a QToolButton instead of a QPushButton outside of the context of a QToolBar? I don't think I fully understand the QAction system yet.

wysota
22nd February 2012, 07:41
QToolButton can take less space and can be associated with a QAction easier than QPushButton.