Results 1 to 9 of 9

Thread: Drawing over a button

  1. #1
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Drawing over a button

    I have a custom button class derived from QPushButton.I want to draw a "/" over this button and some parts of "/" will be outside of this button.When I click on the button,it will emit a right click signal(actually I did signal-slot code and it works correctly).

    I tried creating another custom button class and using qpainter with paintevent.But seems like actually it draws another button over my button,so I can't right click on the button under it.

    How can I make this work?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Drawing over a button

    How can we tell you how to make your code work... We only have the vaguest idea what you have actually done. Perhaps you could show us some code.

  3. #3
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing over a button

    Thanks for your answer.


    months.h :
    Qt Code:
    1. class months:public QPushButton
    2. {
    3.  
    4. Q_OBJECT
    5. int day;
    6.  
    7.  
    8. public:
    9.  
    10. /
    11.  
    12. months(){}
    13. months(QWidget* frame ):QPushButton(frame){}
    14.  
    15.  
    16. void setmonths(months** box2,int month2);
    17.  
    18. void setmonths(int month2);
    19.  
    20. signals:
    21. void rightClickedd();
    22.  
    23. protected:
    24.  
    25. void mouseReleaseEvent(QMouseEvent *e );
    26.  
    27. public slots:
    28. void whenrightclicked();
    29.  
    30. };
    31.  
    32.  
    33.  
    34. class keyday:public QPushButton
    35. {
    36.  
    37. Q_OBJECT
    38. int day;
    39.  
    40.  
    41. public:
    42.  
    43.  
    44.  
    45. keyday(){}
    46. keyday(QWidget* frame ):QPushButton(frame){}
    47.  
    48.  
    49. void paintEvent(QPaintEvent*);
    50.  
    51.  
    52. };
    To copy to clipboard, switch view to plain text mode 


    months.cpp :

    Qt Code:
    1. months* box[2];
    2.  
    3. void keyday::paintEvent(QPaintEvent *)
    4. {
    5. QPainter painter(this);
    6. painter.setPen(Qt::white);
    7. painter.setFont(QFont("Arial", 15));
    8. painter.drawText(rect(), Qt::AlignCenter, "/");
    9.  
    10. }
    11.  
    12.  
    13.  
    14. void months::setmonths(int month2)
    15. {
    16. keyday* button1 =new keyday(frame2);
    17. box[1]= new months(frame2);
    18.  
    19. box[1]->setGeometry(47,2,70,15)
    20. button1->setGeometry(47,2,70,25);
    21. button1->show();
    22. }
    To copy to clipboard, switch view to plain text mode 


    I am trying to paint "/" over box[1] and I tried using QPainter,but seems like it needs to be painter over a widget.So I created a new class derived from QPushButton(also tried QLabel).Now it paints "/" over box[1] but I can't click on box[1] because when I click on box[1] it actually clicks on button1.I tried QPainter without using it inside a derived class' QPaintEvent,but it doesn't work in that way I think.

    There is wxMemoryDc class in Wxwidgets for doing what I want ,I am looking for a similar thing in Qt.
    Last edited by Awareness; 14th October 2013 at 00:26.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Drawing over a button

    I cannot fathom the logic of your setmonths() function. Nevertheless, drawing a line on top of a button without breaking its function is straightforward enough:
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QPainter>
    4.  
    5. class CrossedButton: public QPushButton {
    6. Q_OBJECT
    7. public:
    8. explicit CrossedButton(QWidget *p = 0): QPushButton(p) {
    9. setText("Test");
    10. }
    11.  
    12. protected:
    13. void paintEvent(QPaintEvent *event) {
    14. QPushButton::paintEvent(event);
    15.  
    16. QPainter painter(this);
    17. painter.setPen(Qt::red);
    18. painter.setFont(QFont("Arial", 15));
    19. painter.drawText(rect(), Qt::AlignCenter, "/");
    20. // OR
    21. // painter.setRenderHint(QPainter::Antialiasing, true);
    22. // painter.setPen(QPen(Qt::red, 2));
    23. // painter.drawLine(rect().topLeft(), rect().bottomRight());
    24. }
    25. };
    26.  
    27. int main(int argc, char **argv) {
    28. QApplication app(argc, argv);
    29. CrossedButton b;
    30. b.show();
    31. return app.exec();
    32. }
    33. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to ChrisW67 for this useful post:

    Awareness (15th October 2013)

  6. #5
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing over a button

    Thanks for your answer.

    Is it possible to use QPainter for drawing without using it inside a widget?If I use it inside a widget and put that widget on another widget,when I run the program,I can't click on the lower widget.
    Last edited by Awareness; 15th October 2013 at 02:46.

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Drawing over a button

    If I use it inside a widget and put that widget on another widget,when I run the program,I can't click on the lower widget.
    Qt does the sane thing: the top-most widget under the mouse pointer is the one that gets first look at the click event. The widget can chose to ignore the event or, as makes sense for a button, consume it and be "pushed".

    I have given you a button with an overdrawn line that you can click regardless of the line.

    It sounds like you have a poorly designed UI.
    Exactly what effect are you trying to achieve with this line?

  8. The following user says thank you to ChrisW67 for this useful post:

    Awareness (18th October 2013)

  9. #7
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing over a button

    Thanks for your answer.

    Maybe this image can explain my question better:


    paint2.jpg


    How can I do this using QPainter?Please click on the image (to make it larger), in small version the red line's borders are not visible clearly.
    Last edited by Awareness; 16th October 2013 at 01:57.

  10. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Drawing over a button

    One way i can think of is to have the button embedded in QGraphicsScene as QGraphicsProxyWidget (via QGraphicsScene::addWidget). Then you can draw whatever you want on it with QPainter.

  11. The following user says thank you to stampede for this useful post:

    Awareness (18th October 2013)

  12. #9
    Join Date
    Oct 2009
    Posts
    50
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Drawing over a button

    Actually,after I sent my previous message with picture,I thought it was not a good idea to do it like I do it in the picture(at least for my current little project) and I used ChrisW67 's code in his reply,it is working good.But I didn't take my last question back,because I thought I may need the answer in the future and also I wondered if it was possible to do it in Qt.Thank you both ChrisW67 and stampede for your answers.

Similar Threads

  1. Replies: 2
    Last Post: 26th April 2011, 11:44
  2. Replies: 5
    Last Post: 26th January 2011, 19:12
  3. Replies: 6
    Last Post: 21st August 2010, 21:09
  4. Replies: 1
    Last Post: 2nd August 2010, 05:40
  5. drawing a 3d button in 2d view
    By wagmare in forum Qt Programming
    Replies: 3
    Last Post: 3rd May 2009, 13:02

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.