Qt-interest Archive, May 2007
painting problem
Message 1 in thread
I'm new to C++ and Qt. I've been trying to port some code from an app
using Qt 3 to Qt 4. I'm currently using Ubuntu and Qt 4.2.3. The code
below runs under Qt 3, but not Qt 4. The problem occurs in the drawPBS
method when trying to paint pbs. All I get is the error message:
"QPainter::begin: Widget painting can only begin as a result of a
paintEvent."
I'm sure I'm missing something fundamental, but just haven't been able
to find it. Any pointers (no pun intended) would be greatly
appreciated.
This is my first post, so if I've sent too much information, let me
know.
Cheers, Mel
Code follows:
....................................................................
#include <qapplication.h>
#include <qfontdatabase.h>
#include "main_widget.h"
int main (int argc, char **argv)
{
QApplication app(argc, argv);
Main_Widget *w = new Main_Widget;;
app.setStyleSheet("QFrame {border : 1px solid rgb(255,200,55)}");
w->show();
return app.exec();
}
#ifndef SDXCVR_MAINWIDGET_H
#define SDXCVR_MAINWIDGET_H
#include <qwidget.h>
#include <qapplication.h>
#include <qfont.h>
#include <qlistview.h>
#include <QLabel>
#include <qfile.h>
#include <qpainter.h>
#include <QPaintDevice>
#include <qimage.h>
#include <qdir.h>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPalette>
#include <QFrame>
#include "pbs.h"
#include "sm.h"
class Main_Widget : public QWidget
{
Q_OBJECT
private:
Sm *sm;
PBS *pbs;
QFrame *sFrame;
void updateLayout();
void drawPBS();
public:
Main_Widget(QWidget *parent = 0);
protected:
void paintEvent( QPaintEvent * );
};
#endif
#include "main_widget.h"
Main_Widget::Main_Widget(QWidget *parent)
: QWidget(parent)
{
setFocusPolicy( Qt::TabFocus );
setPalette(QPalette(QColor(0, 0, 0)));
setAutoFillBackground(true);
setMinimumWidth( 650 );
setMinimumHeight( 300 );
setWindowTitle(" Test " );
// sFrame
sFrame = new QFrame( this );
sFrame->setFrameStyle( QFrame::StyledPanel | QFrame::Plain );
// sm
sm = new Sm( sFrame );
sm->setPalette(QColor(20, 20, 50) );
sm->setAutoFillBackground(true);
// PBS
pbs = new PBS( sFrame );
QPalette r(pbs->palette());
r.setColor(QPalette::Background, QColor(100, 0, 0));
pbs->setPalette(r);
pbs->setAutoFillBackground(true);
}
void Main_Widget::paintEvent( QPaintEvent * )
{
printf("paintEvent() \n");
updateLayout();
drawPBS();
}
void Main_Widget::updateLayout()
{
printf("updateLayout() \n");
sFrame->setGeometry(
1,
35,
width() - 2,
height() - 18 - 36 );
sm->setGeometry(
2,
2,
sFrame->width() - 4,
sFrame->height() - 4 - 120 - 15 );
pbs->setGeometry(
2,
sm->height() + 2,
sFrame->width() - 4,
15 );
printf("updateLayout() pbs Geometry \n");
printf(" 2, %d, %d, 15\n", sm->height() + 2, sFrame->width() - 4);
}
void Main_Widget::drawPBS()
{
QPainter p;
p.begin(pbs);
p.eraseRect( 0, 0, pbs->width(), pbs->height() );
p.setPen( Qt::yellow );
p.drawLine(252, 50, 252, 190);
p.drawLine(4,115,70,115);
p.end();
}
#ifndef SDXCVR_PBS_H
#define SDXCVR_PBS_H
#include <qwidget.h>
#include <QMouseEvent>
#include <QPainter>
#include <QPaintDevice>
class PBS : public QWidget
{
Q_OBJECT
public:
PBS(QWidget *parent = 0);
private:
int x0;
protected:
void mousePressEvent( QMouseEvent * );
void mouseMoveEvent( QMouseEvent * );
signals:
void set_lower_pb( int );
void set_upper_pb( int );
void movement( int );
};
#endif
#include "pbs.h"
#include "main_widget.h"
PBS::PBS(QWidget *parent) : QWidget(parent)
{
setMouseTracking( true );
}
void PBS::mouseMoveEvent( QMouseEvent *e )
{
emit movement( e->x() );
}
void PBS::mousePressEvent( QMouseEvent *e )
{
x0 = e->x();
if ( e->button() == Qt::LeftButton )
emit set_lower_pb( x0 );
if ( e->button() == Qt::RightButton )
emit set_upper_pb( x0 );
}
#ifndef SDXCVR_SM_H
#define SDXCVR_SM_H
#include <qwidget.h>
#include <QMouseEvent>
class Sm : public QWidget
{
Q_OBJECT
public:
;
Sm(QWidget *parent = 0);
private:
int mouseMoving;
protected:
void mouseReleaseEvent( QMouseEvent * );
void mouseMoveEvent( QMouseEvent * );
signals:
void tune1( int );
void tune2( int );
void plot( int );
void movement( int );
};
#endif
#ifndef SDXCVR_SM_H
#define SDXCVR_SM_H
#include "sm.h"
Sm::Sm(QWidget *parent) : QWidget(parent)
{
//setMouseTracking( true );
mouseMoving = 0;
}
void Sm::mouseReleaseEvent( QMouseEvent *e )
{
if ( !mouseMoving && e->button() == Qt::LeftButton )
emit tune1( e->x() );
if ( !mouseMoving && e->button() == Qt::RightButton )
emit plot( e->y() );
mouseMoving = false;
}
void Sm::mouseMoveEvent( QMouseEvent *e )
{
static int x0 = 0;
int output;
mouseMoving = true;
if ( x0 - e->x() >= 0 )
output = 1;
else
output = -1;
if ( e->button() == Qt::LeftButton )
emit tune2( output );
else if ( e->button() == Qt::RightButton )
emit tune2( output * 10 );
else if ( e->button() == Qt::MidButton )
emit tune2( output * 100 );
else
emit movement( e->x() );
x0 = e->x();
}
--
[ signature omitted ]
Message 2 in thread
You are trying to repaint pbs widget from MainWindow's paintEvent
method, which is not allowed (each widget should be repainted only
"inside" their paintEvent). Move the code to paint pbs from
MainWindow::paintEvent to PBS::paintEvent (and omit the p.begin(...)
and p.end() calls) and it should work IMHO.
Martin Petricek
On 5/7/07, Melvin G. Seyle, Jr. <melvin.seyle@xxxxxxxxxxx> wrote:
> I'm new to C++ and Qt. I've been trying to port some code from an app
> using Qt 3 to Qt 4. I'm currently using Ubuntu and Qt 4.2.3. The code
> below runs under Qt 3, but not Qt 4. The problem occurs in the drawPBS
> method when trying to paint pbs. All I get is the error message:
> "QPainter::begin: Widget painting can only begin as a result of a
> paintEvent."
>
> I'm sure I'm missing something fundamental, but just haven't been able
> to find it. Any pointers (no pun intended) would be greatly
> appreciated.
>
> This is my first post, so if I've sent too much information, let me
> know.
>
> Cheers, Mel
>
> Code follows:
> ....................................................................
> #include <qapplication.h>
> #include <qfontdatabase.h>
> #include "main_widget.h"
>
>
> int main (int argc, char **argv)
> {
> QApplication app(argc, argv);
> Main_Widget *w = new Main_Widget;;
> app.setStyleSheet("QFrame {border : 1px solid rgb(255,200,55)}");
> w->show();
> return app.exec();
> }
> #ifndef SDXCVR_MAINWIDGET_H
> #define SDXCVR_MAINWIDGET_H
>
> #include <qwidget.h>
> #include <qapplication.h>
> #include <qfont.h>
> #include <qlistview.h>
> #include <QLabel>
> #include <qfile.h>
> #include <qpainter.h>
> #include <QPaintDevice>
> #include <qimage.h>
> #include <qdir.h>
> #include <QKeyEvent>
> #include <QMouseEvent>
> #include <QPalette>
> #include <QFrame>
>
> #include "pbs.h"
> #include "sm.h"
>
> class Main_Widget : public QWidget
> {
>
>
> Q_OBJECT
>
> private:
>
> Sm *sm;
> PBS *pbs;
> QFrame *sFrame;
>
> void updateLayout();
> void drawPBS();
>
> public:
> Main_Widget(QWidget *parent = 0);
>
> protected:
>
> void paintEvent( QPaintEvent * );
>
>
> };
> #endif
>
> #include "main_widget.h"
>
> Main_Widget::Main_Widget(QWidget *parent)
> : QWidget(parent)
> {
>
> setFocusPolicy( Qt::TabFocus );
> setPalette(QPalette(QColor(0, 0, 0)));
> setAutoFillBackground(true);
> setMinimumWidth( 650 );
> setMinimumHeight( 300 );
>
> setWindowTitle(" Test " );
>
> // sFrame
>
> sFrame = new QFrame( this );
> sFrame->setFrameStyle( QFrame::StyledPanel | QFrame::Plain );
>
> // sm
> sm = new Sm( sFrame );
> sm->setPalette(QColor(20, 20, 50) );
> sm->setAutoFillBackground(true);
>
> // PBS
> pbs = new PBS( sFrame );
> QPalette r(pbs->palette());
> r.setColor(QPalette::Background, QColor(100, 0, 0));
> pbs->setPalette(r);
> pbs->setAutoFillBackground(true);
>
> }
>
> void Main_Widget::paintEvent( QPaintEvent * )
> {
> printf("paintEvent() \n");
>
> updateLayout();
> drawPBS();
>
> }
>
> void Main_Widget::updateLayout()
> {
> printf("updateLayout() \n");
>
> sFrame->setGeometry(
> 1,
> 35,
> width() - 2,
> height() - 18 - 36 );
>
> sm->setGeometry(
> 2,
> 2,
> sFrame->width() - 4,
> sFrame->height() - 4 - 120 - 15 );
>
> pbs->setGeometry(
> 2,
> sm->height() + 2,
> sFrame->width() - 4,
> 15 );
> printf("updateLayout() pbs Geometry \n");
> printf(" 2, %d, %d, 15\n", sm->height() + 2, sFrame->width() - 4);
> }
>
>
>
> void Main_Widget::drawPBS()
> {
>
> QPainter p;
> p.begin(pbs);
> p.eraseRect( 0, 0, pbs->width(), pbs->height() );
> p.setPen( Qt::yellow );
> p.drawLine(252, 50, 252, 190);
> p.drawLine(4,115,70,115);
> p.end();
>
> }
> #ifndef SDXCVR_PBS_H
> #define SDXCVR_PBS_H
>
> #include <qwidget.h>
> #include <QMouseEvent>
> #include <QPainter>
> #include <QPaintDevice>
>
>
> class PBS : public QWidget
> {
> Q_OBJECT
>
> public:
>
> PBS(QWidget *parent = 0);
>
> private:
> int x0;
>
> protected:
> void mousePressEvent( QMouseEvent * );
> void mouseMoveEvent( QMouseEvent * );
>
> signals:
> void set_lower_pb( int );
> void set_upper_pb( int );
> void movement( int );
> };
> #endif
>
> #include "pbs.h"
> #include "main_widget.h"
>
> PBS::PBS(QWidget *parent) : QWidget(parent)
> {
> setMouseTracking( true );
> }
>
> void PBS::mouseMoveEvent( QMouseEvent *e )
> {
> emit movement( e->x() );
> }
>
> void PBS::mousePressEvent( QMouseEvent *e )
> {
> x0 = e->x();
>
> if ( e->button() == Qt::LeftButton )
> emit set_lower_pb( x0 );
> if ( e->button() == Qt::RightButton )
> emit set_upper_pb( x0 );
> }
> #ifndef SDXCVR_SM_H
> #define SDXCVR_SM_H
>
> #include <qwidget.h>
> #include <QMouseEvent>
>
> class Sm : public QWidget
> {
> Q_OBJECT
>
> public:
> ;
> Sm(QWidget *parent = 0);
> private:
> int mouseMoving;
>
> protected:
> void mouseReleaseEvent( QMouseEvent * );
> void mouseMoveEvent( QMouseEvent * );
>
> signals:
> void tune1( int );
> void tune2( int );
> void plot( int );
> void movement( int );
> };
> #endif
> #ifndef SDXCVR_SM_H
> #define SDXCVR_SM_H
>
> #include "sm.h"
>
>
> Sm::Sm(QWidget *parent) : QWidget(parent)
> {
> //setMouseTracking( true );
>
> mouseMoving = 0;
> }
>
> void Sm::mouseReleaseEvent( QMouseEvent *e )
> {
> if ( !mouseMoving && e->button() == Qt::LeftButton )
> emit tune1( e->x() );
>
> if ( !mouseMoving && e->button() == Qt::RightButton )
> emit plot( e->y() );
>
> mouseMoving = false;
> }
>
> void Sm::mouseMoveEvent( QMouseEvent *e )
> {
> static int x0 = 0;
> int output;
>
> mouseMoving = true;
>
> if ( x0 - e->x() >= 0 )
> output = 1;
> else
> output = -1;
>
> if ( e->button() == Qt::LeftButton )
> emit tune2( output );
> else if ( e->button() == Qt::RightButton )
> emit tune2( output * 10 );
> else if ( e->button() == Qt::MidButton )
> emit tune2( output * 100 );
> else
> emit movement( e->x() );
>
> x0 = e->x();
> }
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]