Results 1 to 5 of 5

Thread: Help,I want to make a ruler,all the code is here

  1. #1
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Help,I want to make a ruler,all the code is here

    Sorry to my bad english,i want the Rect to fill one color,but it is only a little point.why will
    like that.

    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QPixmap>
    4. #include <QBitmap>
    5. #include <QPoint>
    6. #include <QPalette>
    7. #include <QMouseEvent>
    8. #include <QPainter>
    9.  
    10. class myclass:public QWidget
    11. {
    12. public:
    13. myclass(QWidget *parent = 0);
    14. protected:
    15. void mouseMoveEvent(QMouseEvent *e);
    16. void mousePressEvent(QMouseEvent *e);
    17. private:
    18. QPixmap *pixmap;
    19. QBitmap *bitmap;
    20. QPainter *painter ;
    21. QBrush *brush ;
    22. QPoint last,pos0;
    23. };
    24. myclass::myclass(QWidget *parent)
    25. : QWidget(parent,Qt::X11BypassWindowManagerHint)
    26. {
    27. setGeometry(200,200,800,120);
    28. brush =new QBrush();
    29. brush->setStyle(Qt::SolidPattern) ;
    30. brush->setColor(Qt::gray) ;
    31. pixmap=new QPixmap(800,120);
    32. painter =new QPainter(pixmap) ;
    33.  
    34. //painter->setBackgroundMode(Qt::OpaqueMode);
    35. painter->begin(this) ;
    36. painter->setBackground(*brush) ;
    37. painter->setPen(Qt::red) ;
    38. painter->drawEllipse(10,0,10,10) ;//画了一个小圆圈,运行时可见。
    39. painter->end() ;
    40. QPalette palette;
    41. palette.setBrush(QPalette::Background, QBrush(*pixmap));
    42. setPalette(palette);
    43. setMask(*pixmap);
    44. }
    45. void myclass::mouseMoveEvent(QMouseEvent *e)
    46. {
    47. if (!(e->buttons() & Qt::LeftButton))
    48. return;
    49. QPoint newpos = e->globalPos();
    50. QPoint upleft = pos0 + newpos - last;
    51. move(upleft);
    52. }
    53. void myclass::mousePressEvent(QMouseEvent *e)
    54. {
    55. if (e->button() == Qt::LeftButton)
    56. last = e->globalPos();
    57. pos0 = e->globalPos() - e->pos();
    58. }
    59.  
    60. int main(int argc,char *argv[])
    61. {
    62. QApplication a(argc,argv);
    63. myclass w;
    64. w.showFullScreen();
    65. return a.exec();
    66. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 21st April 2008 at 14:07. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Help,I want to make a ruler,all the code is here

    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.
    Last edited by momesana; 21st April 2008 at 16:01.

  3. The following user says thank you to momesana for this useful post:

    duduqq (22nd April 2008)

  4. #3
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default

    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.

  5. #4
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help,I want to make a ruler,all the code is here

    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 Myclassublic 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();
    }

  6. #5
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Help,I want to make a ruler,all the code is here

    the problem has solutioned,
    I use setMask(pixmap.createHeuristicMask(false));

Similar Threads

  1. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57
  2. problem with linking
    By mickey in forum Qt Programming
    Replies: 49
    Last Post: 12th August 2006, 21:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.