PDA

View Full Version : Qt3: How to update QLabel/QPixmap? Should I even be using those?



ritchan
9th April 2011, 13:23
As I understand it:
QPainter: paints on a QPixmap.
QPixmap: a surface. Displayed by a QLabel. Can be converted into a QImage, where I can do some stuff which I haven't read about yet.
QLabel: used to display a QPixmap.

So I have a button that makes an ellipse with certain parameters. I click it, nothing happens. I resize the window around the areas where it's supposed to be drawn, or minimize and restore the window, and voila there it is:
6212
The white one is the one added by the button, the red ones were drawn inside the constructor of the class that defines the window (I think the parent of this particular tab would be QWidget _guiTab, you'll see that later on maybe).

But I want it to update automatically. I have no idea how to do that (I don't want to make it poll or anything like that), so in lieu of an automatic solution I created an update button. Problem is, only QWidget has an update() function. I would've expected QPixmap, or QLabel to have this function (I don't know if QPixmap has already recognized that the QPainter has already drawn on it, or if QLabel simply hasn't refreshed QPixmap). And it seems it's not really a slot, so I have to make my own wrapper function inside this class that calls _guiTab->update(). It STILL doesn't work. Here's the affected code:

header file:

QLineEdit* _nameInput;

QComboBox* _frequencyBox;
QPixmap *pixmap;
QLabel *pixmaplabel;
QPainter *drawstuff;
QPushButton* _startStopButton;
QPushButton *ellipse;
QPushButton *updateButton;
QPointArray splineone;
QString* _subjectName;

This is all in the constructor:

QFrame *testFrame = new QFrame(_guiTab);
testFrame->setGeometry(350,20,650,620);
testFrame->setFrameShadow(QFrame::Sunken);
testFrame->setFrameShape(QFrame::Panel);

ellipse = new QPushButton("&Ellipse", modeFrame);
ellipse->setGeometry(10,160,100,40);
connect(ellipse,SIGNAL(clicked()),this,SLOT(doimpl antscomeinellipses()));

updateButton = new QPushButton("&Update", modeFrame);
updateButton->setGeometry(50,260,200,40);
connect(updateButton,SIGNAL(clicked()),this,SLOT(u pdatewrapper()));

pixmaplabel = new QLabel(testFrame);
pixmap = new QPixmap(650,600);
pixmaplabel->setPixmap(*pixmap);
pixmaplabel->setAutoResize(TRUE);
drawstuff = new QPainter(pixmap);
QPointArray splineone(4);
splineone[0]=QPoint(50,50);
splineone[1]=QPoint(150,0);
splineone[2]=QPoint(150,150);
splineone[3]=QPoint(400,200);
drawstuff->setPen(Qt::red);
drawstuff->drawRect(10,10,200,100);
drawstuff->drawCubicBezier(splineone);
drawstuff->drawRoundRect(200,180,400,320);
_nameInput = new QLineEdit(modeFrame);
_nameInput->setGeometry(10,50,280,45);
connect(_nameInput,SIGNAL(textChanged ( const QString & )),this,SLOT(nameInputChanged( const QString & )));


and somewhere below that:

void CochlearImplantExperiment_amt::doimplantscomeinell ipses()
{
drawstuff->setPen(Qt::white);
drawstuff->drawEllipse(100,100,400,150);
}
void CochlearImplantExperiment_amt::updatewrapper()
{
_guiTab->update();
cout << "wtf";
}


I'm not even sure that this combination of classes is the right thing to use. Previously I used QPainter drawing on a QCanvas, coupled with QCanvasView, but I found that there was no low level access to the points on a curve drawn on the QCanvas. You see, what I want to do is to make a curve/shape, and program a controller that guides a human to trace such a curve using vibrotactile signals. This curve is defined in the program because drawing with a mouse sucks, and besides the shapes that the user will be asked to trace IRL will be predefined shapes anyway. So I was thinking about converting QPixmap into a QImage later, and then operating on pixels of a specific colour in that QImage to make the controller find the path it's supposed to guide the user along. Is that possible with QImage/QPixmap or am I wasting my time?

high_flyer
9th April 2011, 22:31
I click it, nothing happens.
It means the drawing code is not called.
And its not called because the code in not in a paintEvent().
Subclass QLabel and override ite paintEvent(), and put your specialized drawing code there.


QWidget has an update() function. I would've expected QPixmap, or QLabel to have this function
QLabel does.
Read about the principals of OOP, and inheritance.

Why do you use Qt3?
Do you have a special restriction to use Qt3 only?

ritchan
9th April 2011, 23:47
Thanks for the fast reply. I've been stuck for quite a while on this particular problem. Lemme get back to you on that once I've brushed up a lot more on C++...

I can only use Qt 3.3.3, because the code compiles only on VS2003. Qt4 only has a VS2005 plugin, but VS2005 is much stricter, and besides, at one point in time, I tried - thousands of errors here and there thanks to VS2005's stricter code policies, not just in the code I'm responsible for, but also all of its libraries, which are also quite outdated at this point in time. As a university student, I gain precisely zero recognition for rewriting other people's code anyway - so yeah.

high_flyer
10th April 2011, 15:55
I can only use Qt 3.3.3, because the code compiles only on VS2003. Qt4 only has a VS2005 plugin, but VS2005 is much stricter, and besides, at one point in time, I tried - thousands of errors here and there thanks to VS2005's stricter code policies,
I can hardly imagine that code will not compile on 2005 due to "strictness" of the compiler, which does compile on 2003.
C++ hasn't changed THAT much over the two versions, such that ERROR will be given in one version that compiles on the other.
I will accept this for warnings, and warning levels. but not for errors.
The problem is probably more with the way you setup your project than with the move to a "stricter" compiler.
And even if you where correct, why don't you use then MinGW for example?
I would strongly recommend to you to move to Qt4!