PDA

View Full Version : how to create a new function



tommy
9th November 2007, 23:26
Hello,

I have a very simple program below that draws a very simple picture (just one dot) and assigns that picture to a widget. Widget is then added to main layout and displayed. Everything works great. Now I'd like the drawing part to take place in another function. I'd like a function draw() to call from main function, draw this little dot and return to main(). All my attempts to achieve that have failed because I'm not sure how the new function should be declared/created (what arguments to use). Am I supposed to create a new class for that function? Or use canvas? I know this must be a very simple thing to do but I just can't think of it. I'm very thankful for any guidance.

#include <QtGui>
using namespace std;

int main(int argc, char *argv[])
{
QApplication application(argc, argv);
QWidget window;
QVBoxLayout* mainLayout = new QVBoxLayout(&window);
QLabel* pictureLabel = new QLabel;

////start: i want this to be in another function
QPixmap pm(100,100);
QPainter p(&pm);
QPen pen(Qt::white, 10);
p.setPen(pen);
p.drawPoint(20,20);
////end: i want this to be in another function

pictureLabel->setPixmap(pm);
mainLayout->addWidget(pictureLabel);

window.show();
return application.exec();
}

danbetz
10th November 2007, 00:21
I'm not sure I understand what you really intend to do and where the problem is. Is this what you are looking for?



#include <QApplication>
#include <QtGui>

QPixmap drawStuff();
int main(int argc, char *argv[])
{
QApplication application(argc, argv);
QWidget window;
QVBoxLayout* mainLayout = new QVBoxLayout(&window);
QLabel* pictureLabel = new QLabel;

pictureLabel->setPixmap(drawStuff());
mainLayout->addWidget(pictureLabel);

window.show();
return application.exec();
}

QPixmap drawStuff()
{
QPixmap pm(100,100);
QPainter p(&pm);
QPen pen(Qt::white, 10);
p.setPen(pen);
p.drawPoint(20,20);
return pm;
}

tommy
10th November 2007, 03:07
Thanks a lot. You answered my question 100% !!

tommy
10th November 2007, 17:08
--------------------------