PDA

View Full Version : paintEvent problem



di_zou
1st September 2009, 21:36
I am using the Qt plugin for Eclipse and I am following one of the tutorials for Qt 4.3.

I have this:


void cannonfield::setAngle(int angle)
{
if (angle < 5)
angle = 5;
if (angle > 70)
angle = 70;
if (currentAngle == angle)
return;
currentAngle = angle;
update();
emit angleChanged(currentAngle);
}

void cannonfield::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.drawText(200, 200, tr("Angle = ") + QString::number(currentAngle));
}

setAngle is called by:


void lcdrange::on_horizontalSlider_valueChanged(int value)
{
ui.lcdNumber->display(value);
this->setValue(value);
ui.cannon->setAngle(value);
}

The program will run. However, when it is run, nothing is displayed I am trying to get the angle value to be displayed in the "cannonfield" widget. The cannonfield widget is there, but the angle is not displayed. I put:


printf("test");

into the paintEvent method and nothing is displayed in the console when I move the slider around. However, after I close the window, losts of "test"s are displayed.

Also, how does the keyword "emit" work?

Thanks.

victor.fernandez
2nd September 2009, 08:30
Did you try setting a brush?


void cannonfield::paintEvent(QPaintEvent * /* event */)
{
QPainter painter(this);
painter.setPen(Qt::black);
painter.setFont(font());
painter.drawText(200, 200, tr("Angle = ") + QString::number(currentAngle));
}

You should use qDebug() instead of printf() or call fflush(stdout);

emit should work if you connected the signal to some slot and didn't forget to define the Q_OBJECT macro.

di_zou
2nd September 2009, 14:47
Did not work after I set the pen. I think I don't have the signal connected to a slot correctly. I have:


connect(ui.cannon, SIGNAL(angleChanged(int)), this, SLOT(setValue(int)));

In the constructor of my lcdrange class. Also, where is the documentation for qDebug?

victor.fernandez
2nd September 2009, 15:06
Can you post the code of your main widget as well as the full code of the cannonfield?

In the main documentation page (http://doc.trolltech.com/4.5/), at the left side, under "API Reference", click "All functions". Use the search function from your browser to find it in the list. Here is the direct link (http://doc.trolltech.com/4.5/qtglobal.html#qDebug).

di_zou
2nd September 2009, 15:52
Thanks. Code is attached. If you want to see the .h files or the ui files I can upload those as well.

victor.fernandez
2nd September 2009, 16:13
cannonfield is working for me. Aren't you sure the problem is somewhere else?