Results 1 to 6 of 6

Thread: change colors using Qpainter during run time

  1. #1
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X

    Default change colors using Qpainter during run time

    Hi guys,
    i want the color of a rectangle drwan on central widget to change depending on the input i give, the code is wrkin if i embed the color value in de code, but I have no clue how to pass this value at run time and I want the color to change instantaneously....Thanx in advance

    code:


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include"QWidget"
    4. #include <QtGui>
    5. #include "QPixmap"
    6. #include"QPen"
    7. #include<iostream>
    8. using namespace std;
    9. class MyWidget : public QWidget
    10. {
    11. public:
    12. MyWidget();
    13.  
    14. protected:
    15. void paintEvent(QPaintEvent *);
    16.  
    17. };
    18.  
    19. MyWidget::MyWidget()
    20. {
    21.  
    22. }
    23.  
    24.  
    25. void MyWidget::paintEvent(QPaintEvent *event)
    26. {
    27. QRectF rectangle(50,50,300,300);
    28.  
    29. QPainter painter(this);
    30. painter.setRenderHint(QPainter::Antialiasing);
    31. QPen pen(Qt::green, 30, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    32.  
    33. int input=75;
    34. int level=input/20;
    35. int rem=input-level*20;
    36. float a;
    37. switch(level)
    38. {
    39. case 0:
    40.  
    41.  
    42.  
    43. pen.setColor(QColor(0,255,0,255));
    44.  
    45. painter.setPen(pen);
    46.  
    47. a=.1*rem;
    48. painter.setOpacity(a);
    49. painter.drawRect(50, 50, 300, 300);
    50. break;
    51.  
    52.  
    53. case 1:
    54.  
    55. pen.setColor(QColor(rem*6,255,0,255));
    56.  
    57. painter.setPen(pen);
    58. a=.4;
    59. painter.setOpacity(a);
    60. painter.drawRect(50, 50, 300, 300);
    61. break;
    62. case 2:
    63.  
    64. pen.setColor(QColor(127+rem*6,255,0,255));
    65.  
    66. painter.setPen(pen);
    67. a=.6;
    68. painter.setOpacity(a);
    69. painter.drawRect(50, 50, 300, 300);
    70. break;
    71. case 3:
    72.  
    73. pen.setColor(QColor(255,255-rem*6,0,255));
    74.  
    75. painter.setPen(pen);
    76. a=.8;
    77. painter.setOpacity(a);
    78. painter.drawRect(50, 50, 300, 300);
    79. break;
    80. case 4:
    81.  
    82. pen.setColor(QColor(255,127-rem*6,0,255));
    83.  
    84. painter.setPen(pen);
    85. a=.8;
    86. painter.setOpacity(a);
    87. painter.drawRect(50, 50, 300, 300);
    88. break;
    89.  
    90. default:
    91. pen.setColor(QColor(255,0,0,255));
    92.  
    93. painter.setPen(pen);
    94. a=1;
    95. painter.setOpacity(a);
    96. painter.drawRect(50, 50, 300, 300);
    97. break;
    98.  
    99.  
    100.  
    101.  
    102. }
    103.  
    104.  
    105. }
    106.  
    107. MainWindow::MainWindow(QWidget *parent) :
    108. QMainWindow(parent),
    109. ui(new Ui::MainWindow)
    110. {
    111. ui->setupUi(this);
    112. //setStyleSheet("MainWindow {background-image:url('/Users/sandeep_hyd123/Qt sandy/first/photo.jpeg');background-repeat: repeat;background-position: top right;}");
    113. setStyleSheet("MainWindow {border-width: 4px; border-image:url('/Users/sandeep_hyd123/Qt sandy/first/photo.jpeg') 4 4 4 4 stretch stretch;}");
    114.  
    115.  
    116. MyWidget* my_widget = new MyWidget();
    117. QWidget::setFixedSize ( 400, 400 );
    118.  
    119. setCentralWidget(my_widget);
    120. setWindowTitle(QApplication::translate("toplevel", "Main widget"));
    121.  
    122.  
    123.  
    124. }
    125.  
    126. MainWindow::~MainWindow()
    127. {
    128. delete ui;
    129. }
    To copy to clipboard, switch view to plain text mode 

  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: change colors using Qpainter during run time

    What's the difficult bit? Store the desired input as a member variable when your input changes, then call QWidget::update(). In your paint() method use that member variable as your input.

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

    sandeep_hyd123 (9th June 2011)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: change colors using Qpainter during run time

    store the color in your class an use it while painting


    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. ...
    4. public slots:
    5. void setMyPenColor(int x);
    6. private:
    7. QColor myPenColor;
    8. ...
    9.  
    10. };
    11.  
    12. MyWidget::MyWidget()
    13. {
    14. myPenColor = QColor(0,255,0,255);// set default color
    15. }
    16.  
    17.  
    18. void MyWidget::paintEvent(QPaintEvent *event)
    19. {
    20. ...
    21. switch(level)
    22. {
    23. case 0:
    24. pen.setColor(myPenColor); //note this
    25. painter.setPen(pen);
    26. ...
    27. case 1:
    28. pen.setColor(myPenColor); //note this
    29. painter.setPen(pen);
    30. ...
    31. }
    32. ...
    33. }
    34.  
    35. void MyWidget::setMyPenColor(int x)
    36. {
    37. swithc(x)
    38. {
    39. // change color here
    40. case 2: myPenColor = QColor(255,255,0,255); break;
    41. case 4: myPenColor = QColor(255,0,0,255); break;
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

    now call / connect slot to your input widget, take care to modify the slot signature as required by the signal

    Quote Originally Posted by ChrisW67
    then call QWidget::update().
    I missed to mention this, call this in setMyPenColor();

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

    sandeep_hyd123 (9th June 2011)

  6. #4
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: change colors using Qpainter during run time

    @santosh:
    But i am bit confused over the usage of two switch cases... de switch case inside setmypencolor() you mentioned shud be sufficient rite...?
    My doubt is over de usage of signals and slots, will update() function be called evrytime a signal is sent by input widget(shud i create this widget?)..." I missed to mention this, call this in setMyPenColor();" ; i cudnt get this pt... thnx in advance

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: change colors using Qpainter during run time

    I will suggest yet another solution that doesn't involve any extra variables and special code. Simply use QPalette -- let your painting code use one of the colors stored in the palette and then the user can change the color by modifying the palette.

    Qt Code:
    1. void MyWidget::paintEvent(QPaintEvent *pe){
    2. QPainter painter(this);
    3. QPen p;
    4. p.setColor(palette().color(QPalette::Highlight));
    5. painter.setPen(p);
    6. //...
    7. }
    To copy to clipboard, switch view to plain text mode 

    and somewhere else:
    Qt Code:
    1. QPalette p = widget->palette();
    2. p.setColor(QPalette::Highlight, Qt::red);
    3. widget->setPalette(p); // causes update() of the widget and redraw with a modified color
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    sandeep_hyd123 (14th June 2011)

  9. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: change colors using Qpainter during run time

    Quote Originally Posted by sandeep_hyd123
    But i am bit confused over the usage of two switch cases... de switch case inside setmypencolor() you mentioned shud be sufficient rite...?
    No, I just gave an simple implementation example using switch, you should write your code in there

    Quote Originally Posted by sandeep_hyd123
    My doubt is over de usage of signals and slots, will update() function be called evrytime a signal is sent by input widget(shud i create this widget?)
    ...
    No, you have to call update() explicitly in setMyPenColor(), which will cause the widget to be painted with new color

    I missed to mention update() in the previous code example.

    Qt Code:
    1. void MyWidget::setMyPenColor(int x)
    2. {
    3. ...
    4. update();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by wysota
    I will suggest yet another solution that doesn't involve any extra variables and special code. Simply use QPalette...
    This is a simple yet good solution, you can also try exploring this

  10. The following user says thank you to Santosh Reddy for this useful post:

    sandeep_hyd123 (14th June 2011)

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2010, 21:21
  2. Change colors of date of calendarwidget
    By moh.gup@gmail.com in forum Qt Programming
    Replies: 0
    Last Post: 6th April 2010, 20:36
  3. Replies: 5
    Last Post: 20th October 2009, 08:18
  4. How to change some colors in a QPlainTextEdit
    By aarelovich in forum Qt Programming
    Replies: 2
    Last Post: 3rd June 2009, 00:18
  5. Draw a rectangle alternating two colors with qPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2006, 00:12

Tags for this Thread

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.