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. How can I call paintEvent() on custom widget
Forum Updated to NodeBB v4.3 + New Features

How can I call paintEvent() on custom widget

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.0k Views 3 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.
  • I Offline
    I Offline
    iskenderoguz
    wrote on last edited by
    #1

    I created 3 QPushButton and I want to call paintEvent for them. I add some shapes to QPushButton. How can I call paintEvent() for all custom QPushButtons.

    for(int i=0; i<buttonCount; i++)
    {
        QPushButton *btn = new QPushButton(QString::number(i)+". button");
         btn->setGeometry(10*i, 10, 50, 50);
         layout()->addWidget(btn);
    }
    
    
    void MainWindow::paintEvent(QPaintEvent * event)
    {
         //I want to catch all *btns in here. How can I do that?
    }
    
    1 Reply Last reply
    0
    • HamedH Offline
      HamedH Offline
      Hamed
      wrote on last edited by Hamed
      #2

      Hi
      you can simply define QPushButton *btn as a global variable and make instance of it everywhere you need.

      EDIT : I forgot to mention. as @Chris-Kawa It's a bad habit to use global variables. they truly are mess! but it's the easiest way :D

      HamedBabaeyan@yahoo.com
      for more interesting stuff

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        Please don't create global variables. It's a mess from design point of view.

        You shouldn't call paintEvent manually. It is called by Qt when a widget needs to be redrawn. You can call update() to ask Qt to schedule such event. But don't call it from paintEvent() of a parent because it can lead to recursion.

        To answer your question, although it is not appropriate to solve this problem, if you want to get all the children of specific type from a parent you can call findChildren() e.g.

        void MainWindow::someMethod() {
            QList<QWidget*> buttons = findChildren<QPushButton*>();
            //do stuff to buttons
        }
        

        If you want to only get some subset of the buttons you can use the regular expression overload of findChildren() to match some particular names.

        1 Reply Last reply
        1

        • Login

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