Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I create QPushbutton in QPaintEvent
Qt 6.12 LTS is out! See what's new in the release blog

How can I create QPushbutton in QPaintEvent

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.3k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I'm newbie here.
    I wonder how can I create push button in paintEvent.
    I have tried create push button in MainWindow. it work! but
    when I move the code to paintEvent area,it doesn't work for now.

    -1st- button appear and click to exit also work
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)

    {
    QPushButton *quit = new QPushButton("Quit",this);
    quit->setGeometry(50, 40, 75, 30);
    // quit->setIcon(QIcon("NA03.bmp"));
    connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

    }

    -2nd- nothing appear nor click not work!
    void MainWindow::paintEvent(QPaintEvent *e)
    {
    Q_UNUSED(e);
    QPushButton *quit = new QPushButton("Quit",this);
    quit->setGeometry(50, 40, 75, 30);
    //quit->setIcon(QIcon("NA03.bmp"));
    connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    }
    Thank you for all comment.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Simple answer: Don't do that.

      paintEvent, as it names suggest, is for doing some painting. Create your button in the MainWindow constructor.

      I would also recommend reading the examples and demos from Qt's documentation. They'll give you a good starting point.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Thank you for your suggestion.I'll try my best :D

        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
        • Unsolved