tommy
10th November 2007, 21:21
Hi guys,
I learned from danbetz to create new functions and started experimenting with how to turn on the new function using signals/slots. I wrote a code (see below) that displays a button and when the button is clicked something should be drawn above the button. The code compiles fine but no drawing appears when I click the button. Does anyone know why? I'm hugely thankful, as always.
I suspect that the setPixmap(pm) part is the problem, or the picture is not refreshed for some reason, or maybe the slot is declared incorrectly.
////////////////////////////The *.h file//////////////////////////////////////
#ifndef TOM_H
#define TOM_H
#include <QObject>
#include <QPixmap>
class QObject;
class QPixmap;
class MySlot : public QObject
{
Q_OBJECT
public:
MySlot (QObject *parent = 0);
public slots:
QPixmap testDraw();
};
#endif
///////////////////The main file///////////////////////////////////
#include <QtGui>
#include "tom.h"
MySlot::MySlot(QObject *parent):QObject(parent)
{}
QPixmap MySlot::testDraw()
{
QPixmap pm(100,100);
pm.fill();
QPainter p(&pm);
QPen pen(Qt::black, 10);
p.setPen(pen);
p.drawPoint(40,40);
return pm;
}
int main(int argc, char *argv[])
{
QApplication application(argc, argv);
QWidget window;
MySlot myslot;
QVBoxLayout* mainLayout = new QVBoxLayout(&window);
QPushButton* button1 = new QPushButton("Draw");
QLabel* pictureLabel = new QLabel;
QPixmap pm;
QObject::connect(button1, SIGNAL(clicked()), &myslot, SLOT(pm=testDraw()));
pictureLabel->setPixmap(pm);
mainLayout->addWidget(pictureLabel);
mainLayout->addWidget(button1);
window.show();
return application.exec();
}
I learned from danbetz to create new functions and started experimenting with how to turn on the new function using signals/slots. I wrote a code (see below) that displays a button and when the button is clicked something should be drawn above the button. The code compiles fine but no drawing appears when I click the button. Does anyone know why? I'm hugely thankful, as always.
I suspect that the setPixmap(pm) part is the problem, or the picture is not refreshed for some reason, or maybe the slot is declared incorrectly.
////////////////////////////The *.h file//////////////////////////////////////
#ifndef TOM_H
#define TOM_H
#include <QObject>
#include <QPixmap>
class QObject;
class QPixmap;
class MySlot : public QObject
{
Q_OBJECT
public:
MySlot (QObject *parent = 0);
public slots:
QPixmap testDraw();
};
#endif
///////////////////The main file///////////////////////////////////
#include <QtGui>
#include "tom.h"
MySlot::MySlot(QObject *parent):QObject(parent)
{}
QPixmap MySlot::testDraw()
{
QPixmap pm(100,100);
pm.fill();
QPainter p(&pm);
QPen pen(Qt::black, 10);
p.setPen(pen);
p.drawPoint(40,40);
return pm;
}
int main(int argc, char *argv[])
{
QApplication application(argc, argv);
QWidget window;
MySlot myslot;
QVBoxLayout* mainLayout = new QVBoxLayout(&window);
QPushButton* button1 = new QPushButton("Draw");
QLabel* pictureLabel = new QLabel;
QPixmap pm;
QObject::connect(button1, SIGNAL(clicked()), &myslot, SLOT(pm=testDraw()));
pictureLabel->setPixmap(pm);
mainLayout->addWidget(pictureLabel);
mainLayout->addWidget(button1);
window.show();
return application.exec();
}