Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Overriding paintEvent in widget which was created in designer and has own child widgets
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 8.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BootGenius
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      shoyeb
      wrote on last edited by
      #2

      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");

      There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BootGenius
        wrote on last edited by
        #3

        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.begin(widget);
        painter.setPen(Qt::black);
        painter.setFont(QFont("Tahoma", 16));
        painter.drawText(rect, Qt::AlignCenter, text);
        painter.end();
        }@

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          shoyeb
          wrote on last edited by
          #4

          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..

          There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BootGenius
            wrote on last edited by
            #5

            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) :
            QWidget(parent)
            {
            QList<QWidget > widgets = findChildren<QWidget>();
            foreach(QWidget
            widget, widgets)
            {
            widget->installEventFilter(this);
            }
            }

            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
            paintEvent->rect(),
            "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]

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved