Results 1 to 6 of 6

Thread: QPushbutton + QLabel text color

  1. #1
    Join Date
    Oct 2006
    Location
    USA
    Posts
    23
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QPushbutton + QLabel text color

    Hi,

    I want to change (1) background color for a QPushbutton (2) color of text for QPushbutton (3) background color of Qlabel (4) color of text for QLabel. I have been looking at other threads but have not been able to figure out how to best do this.
    I am posting a simple helloworld example for it. Can anyone please guide me how to modify it to do the above things?
    Thanks in advance.

    Regards,
    Ashish

    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QColorGroup>
    4. #include <QLabel>
    5. #include <QWidget>
    6. #include <QHBoxLayout>
    7. #include <QObject>
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12. QWidget *window=new QWidget();;
    13. window->setWindowTitle("my test window");
    14.  
    15. QPushButton *hello=new QPushButton();
    16. hello->setText("Hello world!");
    17. hello->setFont(QFont("Comic Sans MS",30,QFont::Bold));
    18.  
    19. QPalette *pal= new QPalette(hello->palette());
    20. //pal->setColor(QPalette(QColor(250,0,0)));
    21. //pal->setColor(QPalette(?????-
    22. //hello->setPalette(QPalette(Qt::red));
    23.  
    24. QLabel *mylabel=new QLabel();
    25. mylabel->setFont(QFont("Times",20,QFont::Bold));
    26. mylabel->setPalette(QPalette(Qt::red));
    27. mylabel->setText("This is my Label");
    28.  
    29. myl->addWidget(hello);
    30. myl->addWidget(mylabel);
    31. window->setLayout(myl);
    32.  
    33. //mylabel->show();
    34. //hello->show();
    35. window->show();
    36. return app.exec();
    37. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 2nd February 2007 at 23:13. Reason: missing [code] tags

  2. #2
    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: QPushbutton + QLabel text color

    Use stylesheets...

  3. #3
    Join Date
    Oct 2006
    Location
    USA
    Posts
    23
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushbutton + QLabel text color

    Thanks wysota. I am using Qt4.1 which doesn't have the stylesheet functionality.
    Is there any other way I can do this?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPushbutton + QLabel text color

    In Qt 4.1 it's impossible to change the background color of a QPushButton through palettes on WinXP. QWindowsXpStyle uses some native theming engine to draw controls such as a button and modifying the palette does not help. Therefore Qt 4.2 offers stylesheets...

    Qt Code:
    1. QPalette p = label->palette();
    2. p.setColor(Qt::Window, Qt::green); // background
    3. p.setColor(Qt::WindowText, Qt::magenta); // foreground (text)
    4. label->setPalette(p);
    5. label->setAutoFillBackground(true); // child widgets are transparent by default since Qt 4.1
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Oct 2006
    Location
    USA
    Posts
    23
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushbutton + QLabel text color

    Thanks JPN. I tried this code in my example. It gives me 3 errors.
    1) at
    p.setColor(Qt::Window, Qt::green); // background
    saying that:
    error C2664: 'void QPalette::setColor(QPalette::ColorRole,const QColor &)' : cannot convert parameter 1 from 'Qt::WindowType' to 'QPalette::ColorRole'

    2) at
    p.setColor(Qt::WindowText, Qt::magenta); // foreground (text)
    saying that:
    error C2039: 'WindowText' : is not a member of 'Qt'

    3) at
    p.setColor(Qt::WindowText, Qt::magenta); // foreground (text)
    saying that:
    error C2065: 'WindowText' : undeclared identifier

    What am I missing here?

    Thanks,
    Ashish

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPushbutton + QLabel text color

    Sorry, I wrote it on the fly. It should be:
    Qt Code:
    1. p.setColor(QPalette::Window, Qt::green); // background
    2. p.setColor(QPalette::WindowText, Qt::magenta); // foreground (text)
    To copy to clipboard, switch view to plain text mode 
    PS. QPalette, QPalette::ColorRole
    J-P Nurmi

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 10:49
  2. Setting text color on QLabel
    By Yorma in forum Newbie
    Replies: 11
    Last Post: 15th June 2006, 08:25
  3. QLabel background color
    By munna in forum Newbie
    Replies: 3
    Last Post: 1st May 2006, 16:36
  4. QT4 layout of complex dialog is very slow
    By cboles in forum Qt Programming
    Replies: 15
    Last Post: 28th April 2006, 20:57
  5. Change the text color tab page
    By gtthang in forum Qt Programming
    Replies: 4
    Last Post: 18th February 2006, 18:38

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.