Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How can I set a push button

    General and Desktop
    2
    3
    61
    Loading More Posts
    • 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.
    • D
      Demorald last edited by

      I drew some shapes using QPainter and now I want to add a push button on one of them so I can press it and do something. But no matter how I try to do it, using a form editor or directly to the code, it does not appear.

      cpp with shapes (i tried to add a button here directly, but i didn't work, same as in the form editor)

      #include <QPainter>
      #include "colours.h"
      #include <QWidget>
      #include <QPushButton>
      #include <QApplication>
      
      Colours::Colours(QWidget *parent)
          : QWidget(parent)
      { }
      
      void Colours::paintEvent(QPaintEvent *e) {
      
        Q_UNUSED(e);
      
        doPainting();
      }
      
      /*class MyButton : public QWidget {
        
       public:
           MyButton(QWidget *parent = 0);
      };
       
      MyButton::MyButton(QWidget *parent)
          : QWidget(parent) {
       
        QPushButton *quitBtn = new QPushButton("Quit", this);
        quitBtn->setGeometry(50, 40, 75, 30); /
       
        connect(quitBtn, &QPushButton::clicked, qApp, &QApplication::quit);
      }*/
      
      
      
      
      void Colours::doPainting() {
      
        QPainter painter(this);
      
        painter.setRenderHint(QPainter::Antialiasing);
        painter.setPen(QColor("#d4d4d4"));
      
        painter.setBrush(QBrush("#8B4513"));
        painter.drawRect(100, 150, 300, 300);
      
        QPainterPath path1;
      
        painter.setBrush(QBrush("#D2691E"));
        painter.drawChord(100, 50, 300, 200, 0, 16*180);
      
        painter.setBrush(QBrush("#A0522D"));
        painter.drawRect(200, 350, 100, 100);
      
        painter.setBrush(QBrush("#8B4513"));
        painter.drawRect(280, 400, 10, 10);
      
      
      
      }
      
      
      

      main cpp:

      #include <QApplication>
      #include "colours.h"
      #include <QWidget>
      #include <QPushButton>
      
      
      int main(int argc, char *argv[]) {
      
        QApplication app(argc, argv);
      
        Colours window;
      
      
        window.setWindowTitle("Colours");
        window.show();
      
        return app.exec();
      }
      
      

      using the form editor :
      alt text
      What can I do?

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi
        Your class Colours do not seem to call setupUI so im not sure
        the ui is used and hence the button will not show up.
        Have a look at a normal MainWindow and see it has a UI struct and a call to set it up.
        you must have the same for UI file to have effect.

        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
        ---
        
        class MainWindow : public QMainWindow
        {
           ...
        private:
            Ui::MainWindow *ui;
        ...
        
        
        1 Reply Last reply Reply Quote 1
        • D
          Demorald last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • First post
            Last post