Results 1 to 8 of 8

Thread: I can not get a red window

  1. #1
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default I can not get a red window

    Hello
    I can not get a red window pleaze help me.
    Qt Code:
    1. #ifndef E_H
    2. #define E_H
    3.  
    4. #include <QtWidgets>
    5.  
    6. class paint : public QWidget
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. paint(QWidget *parent = 0);
    12.  
    13. protected:
    14. void paintEvent(QPaintEvent *event);
    15. void drawLines(QPainter *qp);
    16.  
    17. };
    18. #endif // E_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "paint.h"
    2. #include <QPainter>
    3.  
    4.  
    5. paint::paint(QWidget *parent)
    6. : QWidget(parent)
    7. {
    8.  
    9. }
    10.  
    11. void paint::paintEvent(QPaintEvent *e)
    12. {
    13. Q_UNUSED(e);
    14. QPainter qp(this);
    15. drawLines(&qp);
    16. }
    17.  
    18. void paint::drawLines(QPainter *qp)
    19. {QColor color(Qt::red);
    20.  
    21. qp->setPen(color);
    22. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "paint.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. paint window;
    9.  
    10. window.resize(280, 270);
    11. window.move(300, 300);
    12. window.setWindowTitle("Lines");
    13. window.show();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: I can not get a red window

    What do you mean?
    This should bring up an empty window, why do you expect a red one?

    Cheers,
    _

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

    rezas1000 (31st August 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: I can not get a red window

    I like the red window.
    But the following code does not.
    Qt Code:
    1. void paint::drawLines(QPainter *qp)
    2. {QColor color(Qt::red);
    3.  
    4. qp->setPen(color);
    5. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: I can not get a red window

    Okay, you have set the pen but where are the drawn lines? IMO, the paint::drawLines() needs some updating. If you want a red window, get the rectangle from the event and qp->fillRect() the rectangle by red color.

  6. The following user says thank you to Radek for this useful post:

    rezas1000 (31st August 2014)

  7. #5
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: I can not get a red window

    Thank you very much
    But,I'm sorry.I do not understand.

  8. #6
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: I can not get a red window

    Setting the pen tells the painter which pen it should use in subsequent painting but it paints nothing. Your drawLines() takes the pen in a hand and - returns without painting anything. If you want a red rectangle (or lines or something else), you need taking the pen in a hand and painting. Something like this:
    Qt Code:
    1. void paint::paintEvent(QPaintEvent *e)
    2. {
    3. QPainter qp(this);
    4. QRect rect(e->rect());
    5.  
    6. qp.fillRect(rect,Qt::red);
    7. }
    To copy to clipboard, switch view to plain text mode 
    and remove the drawLines(). Or, if you plan to use such procedure, pass it the rectangle along with the painter. Also, for better readibility of your code, make the paint:aintEvent() virtual. Yes, I know, it is already virtual because the QWidget:aintEvent() is virtual but specifying what is what allows you to orient in your code better.

  9. The following user says thank you to Radek for this useful post:

    rezas1000 (1st September 2014)

  10. #7
    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: I can not get a red window

    Also, for better readibility of your code, make the paint:aintEvent() virtual. Yes, I know, it is already virtual because the QWidget:aintEvent() is virtual but specifying what is what allows you to orient in your code better.
    Or use "override" specifier if you are using C++11.

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

    rezas1000 (12th September 2014)

  12. #8
    Join Date
    Sep 2014
    Location
    Sydney, Australia
    Posts
    16
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: I can not get a red window

    Do you mean you want a window with a red background?

    I usually find it easiest to give my widgets/windows a custom palette if I just want to change the color only. Something like:

    Qt Code:
    1. QPalette redWindowPalette;
    2. redWindowPalette.setColor(QPalette::Background, QColor(225,75,75,255)); //set background role to a pale red
    3. setPalette(redWindowPalette);//set our widgets palette to the new red background palette
    To copy to clipboard, switch view to plain text mode 

  13. The following user says thank you to Nomad_Tech for this useful post:

    rezas1000 (12th September 2014)

Similar Threads

  1. Replies: 2
    Last Post: 14th January 2013, 07:07
  2. Replies: 1
    Last Post: 10th April 2012, 16:18
  3. Replies: 6
    Last Post: 9th November 2011, 04:31
  4. Replies: 2
    Last Post: 17th February 2011, 12:30
  5. Replies: 11
    Last Post: 4th June 2008, 07:22

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.