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. Draw a point when button is clicked

Draw a point when button is clicked

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 5 Posters 1.1k Views 2 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.
  • M Offline
    M Offline
    mitrovicl
    wrote on last edited by
    #1

    So, I created 2 functions: onClick() - function that executes when the button is clicked, and drawPoint(int x,int y).
    onClick function calls the drawPoint function with x and y parameters, but when I run the program, and I click the button, it says:
    "QPainter::drawPoints: Painter not active".
    Here's my mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QPushButton *button = QMainWindow::findChild<QPushButton *>("pushButton");
        connect(button, SIGNAL(released()), SLOT(onClick()));
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::drawPoint(int x, int y) {
    
        QPainter painter;
        painter.drawPoint(x,y);
    
    }
    
    void MainWindow::onClick() {
        QPoint point = QCursor::pos();
        int x = point.x();
        int y = point.y();
        drawPoint(x,y);
    }
    
    

    I'm a total beginner in Qt, and have no idea what's going on. I've searched on this forum, but I couldn't find anything.

    JonBJ Pl45m4P 2 Replies Last reply
    0
    • M mitrovicl

      So, I created 2 functions: onClick() - function that executes when the button is clicked, and drawPoint(int x,int y).
      onClick function calls the drawPoint function with x and y parameters, but when I run the program, and I click the button, it says:
      "QPainter::drawPoints: Painter not active".
      Here's my mainwindow.cpp:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          QPushButton *button = QMainWindow::findChild<QPushButton *>("pushButton");
          connect(button, SIGNAL(released()), SLOT(onClick()));
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::drawPoint(int x, int y) {
      
          QPainter painter;
          painter.drawPoint(x,y);
      
      }
      
      void MainWindow::onClick() {
          QPoint point = QCursor::pos();
          int x = point.x();
          int y = point.y();
          drawPoint(x,y);
      }
      
      

      I'm a total beginner in Qt, and have no idea what's going on. I've searched on this forum, but I couldn't find anything.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @mitrovicl
      You can only draw a point in a paint event.

      Basically, can you pick something different from this to do? Widget applications are not usually about drawing points. Depends what you are ultimately trying to achieve.

      M 1 Reply Last reply
      2
      • JonBJ JonB

        @mitrovicl
        You can only draw a point in a paint event.

        Basically, can you pick something different from this to do? Widget applications are not usually about drawing points. Depends what you are ultimately trying to achieve.

        M Offline
        M Offline
        mitrovicl
        wrote on last edited by
        #3

        @JonB Thanks for the answer, I'm trying to create CPS (Clicks per second) test. And I want every click to leave a "trail". I'll look into pain events.

        JonBJ 1 Reply Last reply
        0
        • M mitrovicl

          @JonB Thanks for the answer, I'm trying to create CPS (Clicks per second) test. And I want every click to leave a "trail". I'll look into pain events.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @mitrovicl
          Maybe you might be interested in examples which come with Qt:

          Basic Drawing Example

          Scribble Example

          Search for paintEvent in them.

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

            Hi and welcome to devnet,

            The first thing: you cannot paint on a widget outside the paint event.

            Second, please read the QPainter documentation. It provides a lot of information as well as links to various examples.

            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
            2
            • M mitrovicl

              So, I created 2 functions: onClick() - function that executes when the button is clicked, and drawPoint(int x,int y).
              onClick function calls the drawPoint function with x and y parameters, but when I run the program, and I click the button, it says:
              "QPainter::drawPoints: Painter not active".
              Here's my mainwindow.cpp:

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
              
                  QPushButton *button = QMainWindow::findChild<QPushButton *>("pushButton");
                  connect(button, SIGNAL(released()), SLOT(onClick()));
              
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              void MainWindow::drawPoint(int x, int y) {
              
                  QPainter painter;
                  painter.drawPoint(x,y);
              
              }
              
              void MainWindow::onClick() {
                  QPoint point = QCursor::pos();
                  int x = point.x();
                  int y = point.y();
                  drawPoint(x,y);
              }
              
              

              I'm a total beginner in Qt, and have no idea what's going on. I've searched on this forum, but I couldn't find anything.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @mitrovicl said in Draw a point when button is clicked:

              QPushButton *button = QMainWindow::findChild<QPushButton *>("pushButton");

              I assume you have a mainWindow.ui (modified with QtDesigner), then this is not necessary.
              Just access you button by using your ui pointer.

              ui->pushButton
              

              connect(button, SIGNAL(released()), SLOT(onClick()));

              and better use the updated, Qt5 function-based "Signal & Slot"-syntax.

              connect(ui->pushButton, &QPushButton::released, this, &MainWindow::onClick);
              
              • https://wiki.qt.io/New_Signal_Slot_Syntax

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              3
              • S Offline
                S Offline
                SusanCNewton
                wrote on last edited by
                #7
                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