PDA

View Full Version : QTDesigner + QPaint [QT 4.4]



pippofrank
14th November 2008, 17:39
Hi, i have i little problem in painting with qt.
This is the situation: i draw my ui application using QTDesigner, so this is my code:

-----myqtapp.h-----


#include "ui_myqtapp.h"
class myQtApp : public QWidget, private Ui::myQtAppDLG
{
Q_OBJECT
public:
myQtApp(QWidget *parent = 0);
public slots:
void doSomething();
};
-----myqtapp.cpp--------


#include <QtGui>
#include "myqtapp.h"
myQtApp::myQtApp(QWidget *parent)
{
setupUi(this);

connect( btnCrea, SIGNAL( clicked() ), this, SLOT( doSomething() ) );
}

void myQtApp::doSomething()
{
//here i want to paint for example a simple point in my ui. In my UI there is a widget called "widget".

}
Can someone help me? i try many solution as QPaint, or creating a new Widget, but nothing...
Thanks

wysota
15th November 2008, 08:59
You have to do the painting in the paintEvent() of the widget you want to paint on or you can apply an event filter to the widget so that you can intercept the event and do the painting elsewhere. Alternatively you can paint on a pixmap at an arbitrary moment and display it on a QLabel. The painting itself should be done using QPainter.

pippofrank
18th November 2008, 19:56
ok, i insert this code in my file


void QWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 30));
painter.drawText(rect(),
Qt::AlignCenter, "Qt");
}



but it's not clear how to call this event...
I try in many modes, but nothing, the widget doesn't display nothing

wysota
18th November 2008, 20:41
You don't call it - Qt does. You just have to reimplement the paintEvent virtual method in your class.

pippofrank
9th December 2008, 21:55
hi everyone,
now i'm able to draw something on my widget, but i can't create a function (connected to a button) called doSomething() that draw something of new in my widget. How can i call the paintevent method in my code??
thanks:o



#include <QtGui>
#include "test.h"

Test::Test(QWidget *parent)
: QWidget(parent)
{
setupUi(this);
m_drawer->installEventFilter(this);
connect(pushButton, SIGNAL( clicked()), this, SLOT( doSomething() ));
}


void Test::doSomething()
{
i want to call a method that paint something of new on the widget!!
}

bool Test::eventFilter(QObject *o, QEvent *e)
{
if (o == m_drawer && e->type() == QEvent::Paint) {
QPainter painter(m_drawer);
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 30));
painter.drawText(m_drawer->rect(), Qt::AlignCenter, "Qt");
return true;
}
return QWidget::eventFilter(o, e);
}


void Test::paintEvent(QPaintEvent *event){

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 15, Qt::SolidLine, Qt::RoundCap,Qt::MiterJoin));
painter.setBrush(QBrush(Qt::blue, Qt::DiagCrossPattern));
painter.setPen(QPen(Qt::yellow, 5, Qt::SolidLine, Qt::RoundCap,Qt::MiterJoin));
painter.translate(100.0,100.0);
painter.drawPie(0, 0, 300, 300, 60 * 16, 270 * 16);
painter.drawEllipse(30,30,60,60);

}

wysota
10th December 2008, 00:02
QWidget::update()

pippofrank
12th December 2008, 17:16
thank you!!
I've another problem now.


distanzaEuclidea[j] = Math::Sqrt((nodi[i].x - nodi[j].x) * (nodi[i].x - nodi[j].x) + Math::Pow((nodi[i].y - nodi[j].y), 2));

i want to use mathematic functions of c++ library, but i can't include them in my project.
I try
#include <cmath>
#include "match.h"
but the compiler give me always error;
What's the problem??

rexi
12th December 2008, 19:17
#include "match.h"

It would be math.h, or is that just a typo in your post?

Anyway, what error does the compiler give?

pippofrank
12th December 2008, 19:32
sorry,
#include "math.h"

the compiler say:
"Math has not been declared"
"Pow undeclared"
"Sqtr undeclared"

rexi
12th December 2008, 21:07
Well, that figures. math.h only declares functions like pow, sqrt etc. No "Math" namespace or anything like that. So the problem is not including the headers, but your attempt of using the defined functions. Just drop the "Math::" part and write the function names in all lower case, that should do the trick.

wysota
13th December 2008, 17:45
<cmath> uses the std:: namespace hence it is std::sqrt(). Sometimes it is worth looking into the source (or in this case header) files...