Important: Please read the Qt Code of Conduct -

Overriding paintEvent in widget which was created in designer and has own child widgets

  • Can anybody help me. I am developing an application. It has a widget created in designer. It has QTableView widget and 3 QLabel widgets. I want to display some text, when a model of QTableView has 0 rows. For example "There is no rows in your table". I have overrided paintEvent for my widget. In this event I am drawing fill rectangle with text. But the problem is in child widgets. Child widgets have own paintEvent functions which are called always. I need that these functions are never called when my model has 0 rows. I have tried to use installEventFilter for all child widgets. But it has not helped.
    Thanks in advance

  • if u are using qlabel widget then u can daisplay the text u want by setting the text of label using setText method.

    QLabel* label;
    label = new QLabel(this);
    // 'this' is pointer to main widget.

    label->setText("your text");

  • No. There is not what I want. It's just simple.
    I have several widgets with the same behaviours. For example: one widget display users, second display cars and so on. Every widget has own QTableView and. Also these widgets have another gui elements like labels combo boxes and so on. I want manually paint the whole widget (with child elements) when QTableView's model has 0 row. I want all my widgets have the same behavior and are painted with one function like:
    @void CommonFunctions::paintEmptyWidget(QWidget* widget,
    QRect rect,
    QString text,
    QString noteText)
    QPainter painter;
    painter.setFont(QFont("Tahoma", 16));
    painter.drawText(rect, Qt::AlignCenter, text);

    This function will called in paintEvent(QPaintEvent*) of every widget. if I override paintEvent in a simple widget (which was not created in Designer) it works fine. But if I override paintEvent in widget with UI class it works not so as I want. In this case paintEvent works, but besides this, paintEvent functions of all child widgets are called. But I don't want it.

  • then u need to override the paint event of each child widget...
    i am not sure that this will work but atleast u can try..

  • Yes. But I can't override the paintEvent of each child widget, because they were created in Designer (with UI). So maximum I can do - is try to install eventFilter on every widget like this:
    @MyWidget::MyWIdget(QWidget parent) :
    QList<QWidget > widgets = findChildren<QWidget>();
    widget, widgets)

    MyWidget::eventFilter(QObject* object, QEvent* event)
    if(event->type() == QEvent::Paint)
    QPaintEvent* paintEvent = static_cast<QPaintEvent*>(event);
    if(_rowCount == 0)
    paintEmptyWidget(widget, /// Function like I describe above
    "Emty text",
    "Note text");
    return true;
    return QWidget::eventFilter(object,event);
    But it doesn't work as I need.

    [quote author="shoyeb" date="1329193466"]then u need to override the paint event of each child widget...
    i am not sure that this will work but atleast u can try..[/quote]

Log in to reply