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 set a push button

How can I set a push button

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 248 Views
  • 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 Offline
    D Offline
    Demorald
    wrote on last edited by
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • D Offline
        D Offline
        Demorald
        wrote on last edited by
        #3
        This post is deleted!
        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