PDA

View Full Version : Help,I want to make a ruler,all the code is here



duduqq
21st April 2008, 14:13
Sorry to my bad english,i want the Rect to fill one color,but it is only a little point.why will
like that.


#include <QApplication>
#include <QWidget>
#include <QPixmap>
#include <QBitmap>
#include <QPoint>
#include <QPalette>
#include <QMouseEvent>
#include <QPainter>

class myclass:public QWidget
{
public:
myclass(QWidget *parent = 0);
protected:
void mouseMoveEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
private:
QPixmap *pixmap;
QBitmap *bitmap;
QPainter *painter ;
QBrush *brush ;
QPoint last,pos0;
};
myclass::myclass(QWidget *parent)
: QWidget(parent,Qt::X11BypassWindowManagerHint)
{
setGeometry(200,200,800,120);
brush =new QBrush();
brush->setStyle(Qt::SolidPattern) ;
brush->setColor(Qt::gray) ;
pixmap=new QPixmap(800,120);
painter =new QPainter(pixmap) ;

//painter->setBackgroundMode(Qt::OpaqueMode);
painter->begin(this) ;
painter->setBackground(*brush) ;
painter->setPen(Qt::red) ;
painter->drawEllipse(10,0,10,10) ;//画了一个小圆圈,运行时可见。
painter->end() ;
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(*pixmap));
setPalette(palette);
setMask(*pixmap);
}
void myclass::mouseMoveEvent(QMouseEvent *e)
{
if (!(e->buttons() & Qt::LeftButton))
return;
QPoint newpos = e->globalPos();
QPoint upleft = pos0 + newpos - last;
move(upleft);
}
void myclass::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
last = e->globalPos();
pos0 = e->globalPos() - e->pos();
}

int main(int argc,char *argv[])
{
QApplication a(argc,argv);
myclass w;
w.showFullScreen();
return a.exec();
}

momesana
21st April 2008, 16:31
which rect to fill with which color? gray?
What do you mean with dot? I see a red circle which you painted onto the pixmap. So where is the problem?

p.s.
- bitmaps, pixmaps, brushes, colors etc. are all value types. they are passed by reference by qt functions so there is no need to use/pass pointers here.
- By Convention Class names start with Uppercase letters ( MyClass instead of myclass)
- QObject derived classes should define QObject even if they do not intend to use SIGNALS and SLOTS.

duduqq
22nd April 2008, 02:54
Sorry to my bad english,may be you don't understant what i mean.I am very sorry.
I want to paint the rect of the pixmap (800,200) with the brush,but it don't work,it is only can see the Ellipse(10,0,10,10) and some other color point.I want the background is no transparence,and i can draw line in the rect.

duduqq
22nd April 2008, 05:23
now I my code is like that,I can fill the rect with the brush and can draw a Ellipse,but the horizontal and vertical line it can work ,the horizontal and vertical line is transparent,why will like that,
#include <QApplication>
#include <QWidget>
#include <QPixmap>
#include <QBitmap>
#include <QPoint>
#include <QPalette>
#include <QMouseEvent>
#include <QPainter>

class Myclass:public QWidget
{
public:
Myclass(QWidget *parent = 0);
protected:
void mouseMoveEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
private:
QPainter *painter ;
QPoint last,pos0;
};
Myclass::Myclass(QWidget *parent)
: QWidget(parent,Qt::X11BypassWindowManagerHint)
{
setGeometry(20,300,1000,800);
QBrush brush;
brush.setStyle(Qt::SolidPattern) ;
brush.setColor(Qt::gray) ;
QPixmap pixmap(800,120);
painter =new QPainter(&pixmap) ;
painter->save();
painter->fillRect(0,0,800,120,brush) ;

//painter->setBackgroundMode(Qt::OpaqueMode);
//painter->begin(this) ;
//painter->setBackground(brush) ;
painter->setPen(Qt::red) ;
painter->drawRect(0,0,1000,120) ;
painter->drawLine(10,20,10,0) ;
painter->restore();

//painter->rotate(30) ;
//painter->drawPixmap(10,10,pixmap) ;
painter->end() ;

QPalette palette;
//palette.setColor(QPalette::Background,QColor(142,0 ,0)) ;
palette.setBrush(QPalette::Background, QBrush(pixmap));
setPalette(palette);
//setMask(pixmap) ;
setMask(pixmap.createHeuristicMask());
}
void Myclass::mouseMoveEvent(QMouseEvent *e)
{
if (!(e->buttons() & Qt::LeftButton))
return;
QPoint newpos = e->globalPos();
QPoint upleft = pos0 + newpos - last;
move(upleft);
}
void Myclass::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
last = e->globalPos();
pos0 = e->globalPos() - e->pos();
}

int main(int argc,char *argv[])
{
QApplication a(argc,argv);
Myclass w;
//w.show() ;
w.showFullScreen();
return a.exec();
}

duduqq
22nd April 2008, 08:08
the problem has solutioned,
I use setMask(pixmap.createHeuristicMask(false));