How can I create QPushbutton in QPaintEvent



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


  • Lifetime Qt Champion

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.