Results 1 to 3 of 3

Thread: Draw a shape upon clicking a button

  1. #1
    Join Date
    Jul 2013
    Location
    London
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Draw a shape upon clicking a button

    Hi, I'm new to Qt so please bare with me for a second here, and thank you for reading onward.

    I'm trying to create a simple application in Qt Creator 5.1.0, which displays a 'Draw' button and upon clicking it will draw something. For the button: I just drag & drop a simple pushButton in Qt Designer.

    Here is my header mydrawbutton.h
    Qt Code:
    1. #ifndef MYDRAWBUTTON_H
    2. #define MYDRAWBUTTON_H
    3.  
    4. #include <QMainWindow>
    5. #include <QPainter>
    6.  
    7. #include "ui_mydrawbutton.h"
    8.  
    9.  
    10. class mydrawbutton : public QMainWindow, public Ui::mydrawbutton
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit mydrawbutton(QWidget *parent = 0);
    16. ~mydrawbutton();
    17. protected:
    18. void drawShape(QPaintEvent *);
    19.  
    20. private slots:
    21. void draw();
    22.  
    23. private:
    24. Ui::mydrawbutton *ui;
    25. bool m_draw;
    26. };
    27.  
    28. #endif // MYDRAWBUTTON_H
    To copy to clipboard, switch view to plain text mode 

    and source mydrawbutton.cpp:
    Qt Code:
    1. #include <QPushButton>
    2.  
    3. #include "mydrawbutton.h"
    4.  
    5.  
    6. mydrawbutton::mydrawbutton(QWidget *parent) :
    7. QMainWindow(parent)
    8. {
    9. setupUi(this);
    10. m_draw = false;
    11. connect(drawButton, SIGNAL(clicked()), this, SLOT(draw()));
    12. }
    13.  
    14. mydrawbutton::~mydrawbutton()
    15. {
    16. delete ui;
    17. }
    18.  
    19.  
    20.  
    21. void mydrawbutton::drawShape(QPaintEvent *)
    22. {
    23. QPainter painter(this);
    24. if (!m_draw){
    25. painter.setPen( Qt::green );
    26. painter.setBrush( Qt::green );
    27. painter.drawRect(10, 10, 100, 100);
    28. }
    29. }
    30.  
    31. void mydrawbutton::draw()
    32. {
    33. m_draw = !m_draw;
    34. update();
    35. }
    To copy to clipboard, switch view to plain text mode 

    The problem is: when compile it does display the 'Draw' button, however nothing happened when clicking it. I've spent quite sometime reading around and trying to fix it, but haven't been able to make it work. Hope someone can help me fix it. Oh and if I'm missing any bits of information that you need, please tell me. Thank you!

  2. #2
    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: Draw a shape upon clicking a button

    I am not sure what you are expecting.

    Your slot changes the value of m_draw and then tells your main window subclass to update. Since m_draw is not used anywhere in drawing code, the window will be drawn exactly as it was before.

    It looks like you want to the drawShape() method to be executed but you don't call it from anywhere.

    A naive approach would be to call it from paintEvent(), but you really don't want to do that for a QMainWindow subclass.

    So create a simple QWidget subclass that does the slot and drawing an put an instance if it as the main window's central widget.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2013
    Location
    London
    Posts
    10
    Thanks
    2
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Draw a shape upon clicking a button

    Ah, I see the problem here. Thanks very much for your answer, I'll try to work it out.

    Cheers!

Similar Threads

  1. Flick charm not working when clicking over a button.
    By alitoh in forum Qt Programming
    Replies: 0
    Last Post: 9th January 2012, 13:52
  2. how to get the accept signal by clicking Apply button
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 6th January 2012, 01:44
  3. How to make the push button to curly shape?
    By harish in forum Qt Programming
    Replies: 3
    Last Post: 29th November 2011, 17:21
  4. Draw shape on mousepress?
    By Niamita in forum Qt Programming
    Replies: 4
    Last Post: 7th June 2011, 06:59
  5. shape of push button
    By Seema Rao in forum Qt Programming
    Replies: 23
    Last Post: 2nd April 2008, 01:05

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.