PDA

View Full Version : drawing something by pressing a pushButton



kadaj
7th July 2013, 10:33
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::paintEvent(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?!:confused:

p3c0
7th July 2013, 11:50
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.

aamer4yu
7th July 2013, 11:59
Where do you want to draw on button click ? Is it the button itself or another widget ?

kadaj
7th July 2013, 12:32
i want to draw it somewhere on the mainWindow ( i dont know any other solution )

how can i override the paintEvent for that?!

p3c0
7th July 2013, 12:49
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.

anda_skoa
7th July 2013, 13:48
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,
_

kadaj
7th July 2013, 13:48
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?! :\

p3c0
7th July 2013, 14:04
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::paintEvent(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::on_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

kadaj
7th July 2013, 14:06
i got it! thanks very very much! :)

anda_skoa
8th July 2013, 08:41
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,
_