Results 1 to 10 of 10

Thread: drawing something by pressing a pushButton

  1. #1
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default drawing something by pressing a pushButton

    Hi, im a new user in Qt
    i want my program draws something by QPainter but when the user clicks the pushButton
    i searched the net and i found for QPainter we should have void paintEvent(QPaintEvent *event) in the program.h and the program:aintEvent(QPaintEvent *event){...} in the program.cpp
    and i didnt found anything else for using QPainter

    but what i want to do is in the "entryPushButton slot" i use QPainter

    how can i do this?!

  2. #2
    Join Date
    May 2010
    Posts
    19
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: drawing something by pressing a pushButton

    kadaj,

    1. What do you want to do exactly?
    2. Have you subclassed QPushButton ?
    If yes , then there you have to override the paintevent method and write your own code there.
    3. Have you subclassed QDialog ?
    If you want to draw somthing on a QDialog which has a QPushbutton, then send clicked signal to QDialog.
    This QDialog will have a overridden paintevent method where you can draw whatever you want to.

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawing something by pressing a pushButton

    Where do you want to draw on button click ? Is it the button itself or another widget ?

  4. #4
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: drawing something by pressing a pushButton

    i want to draw it somewhere on the mainWindow ( i dont know any other solution )

    how can i override the paintEvent for that?!

  5. #5
    Join Date
    May 2010
    Posts
    19
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: drawing something by pressing a pushButton

    Kadaj,

    What do you want to draw ? Is it some custom drawing. You can add widgets though on it. Use stylesheets to change their colors etc..
    If you want to override paintevent just add paintevent declaration in the mainwindow.h header file . Right click on that line and choose Refactor > Add definition to mainwindow.cpp.
    The definition will be added to mainwindow.cpp.
    Then in paintevent method in mainwindow.cpp use a QPainter add draw whatever you want using its methods like drawPixmap, drawLine, drawPolygon, drawRect etc..
    See http://doc.qt.digia.com/4.6/qpainter.html fo more details.

  6. The following user says thank you to p3c0 for this useful post:

    kadaj (7th July 2013)

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: drawing something by pressing a pushButton

    There are two ways to do that:
    1) have the slot store whatever is needed to know what to draw in some member variables of the widget you are drawing on and then call update()
    This triggers invocation of paintEvent() in which you can then take this stored information and perform your drawing operation

    2) draw in the slot on a pixmap instead of on the widget and then again call update. in paintEvent() you then draw the pixmap.

    Obviously for the latter approach you can also draw in a pixmap and use an existing widget that can display a pixmap, e.g. QLabel

    Cheers,
    _

  8. #7
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: drawing something by pressing a pushButton

    thanks everyone
    but is it possible when the user pushes the pushbutton this function executed, i havent understand how should i write that, if i have to override it, can you plz write the code ( just to understand how ) here for me?! :\

  9. #8
    Join Date
    May 2010
    Posts
    19
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: drawing something by pressing a pushButton

    Well something like this:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    m_clicked = false;

    }

    void MainWindow:aintEvent(QPaintEvent *)
    {
    QPainter p(this);
    if(m_clicked)
    p.setBrush(Qt::red);
    else
    p.setBrush(Qt::blue);
    p.drawRect(rect());
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow:n_pushButton_clicked()
    {
    m_clicked = true;
    update();
    }

    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QPainter>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    void paintEvent(QPaintEvent *);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    bool m_clicked;
    };

    #endif // MAINWINDOW_H

  10. The following user says thank you to p3c0 for this useful post:

    kadaj (7th July 2013)

  11. #9
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: drawing something by pressing a pushButton

    i got it! thanks very very much!

  12. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: drawing something by pressing a pushButton

    Just some general observations:

    - do not override paintEvent of a QMainWindow. the main window's content is the widget set as central widget
    - do not use designer auto connect feature, use actual connect() instead. auto connect is based on object names and will break if you rename the object but forget to also rename the slot.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 29th April 2011, 08:54
  2. [SOLVED] Quit the program by pressing ESC
    By ale6111 in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2010, 14:45
  3. Replies: 2
    Last Post: 8th October 2008, 14:18
  4. Dialog closes on pressing Esc key?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 3rd July 2008, 08:53
  5. closing of window on pressing ESC key
    By raghvendramisra in forum Newbie
    Replies: 2
    Last Post: 7th January 2008, 02:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.