TheMagician
TheMagician

Reputation: 1856

Putting QWidgets inside a text field

I'd like to implement a text field in Qt that replicates the "Tags" field on stackoverflow (when submitting a question). Certain keywords would be replaced by widgets and if I backspace a widget it should be replaced with the text that was typed to create that widget. How could I accomplish something like this? Thanks.

Upvotes: 0

Views: 80

Answers (1)

JefGli
JefGli

Reputation: 791

You can start by inheriting a QWidget and implementing the KeyPress and KeyRelease events. Basically mimicking a QLineEdit widget. So you might need to look for re-usable code to avoid a lot of work, like inheriting QLineEdit and overwriting paintEvent().

Then, in your paintEvent, you use the painter to render the non-widget text and your widgets.

pseudocode:

SomeWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    foreach (object ; objectsToDraw) {
        if (isText) {
            painter.drawText();
        } else if (isWidget) {
            widget.render(&painter);
        }
    }
}

Upvotes: 1

Related Questions