[Interest] Writing custom Qt Widgets is overly complicated
d3fault
d3faultdotxbe at gmail.com
Wed Apr 29 00:36:18 CEST 2020
On 4/28/20, Jonathan Purol <contact at folling.de> wrote:
> The last approach seemed rather clever, and it worked pretty well. I
> could put widgets into the textfield and align them at the left. First I
> tried to do this manually, then I opted for an HBoxLayout.
I don't really understand where you're having trouble. The text
margins? I don't see why those should be dynamic. Set them to a low
number (or zero) and be done with it.
This is how I'd implement your Tag Text Field:
//pseudo-code (also it's static, not dynamic: just showing the end
resulting layout):
class TagTextField : public QWidget
{
Q_OBJECT
public:
TagTextField(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout *myLayout = new QHBoxLayout();
myLayout->addWidget(new TagWidget("Some Tag"), 0, Qt::AlignLeft);
myLayout->addWidget(new TagWidget("Another Tag"), 0, Qt::AlignLeft);
myLayout->AddWidget(new QLineEdit(), 1, Qt::AlignRight); //the
1 here means stretch
setLayout(myLayout);
}
};
class TagWidget : public QWidget
{
Q_OBJECT
public:
TagWidget(const QString &tag, QWidget *parent = nullptr)
: QWidget(parent)
{
QHBoxLayout *myLayout = new QHBoxLayout();
myLayout->addWidget(new QLabel(tag), 0 /*you might want to use
1 here for stretch*/, Qt::AlignLeft);
myLayout->addWidget(new QPushButton("x"), 0, Qt::AlignRight);
setLayout(myLayout);
}
};
d3fault
More information about the Interest
mailing list