Results 1 to 3 of 3

Thread: QSpinBox setting buttons with using stylesheets

  1. #1
    Join Date
    Nov 2010
    Location
    Zawiercie, Poland
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Angry QSpinBox setting buttons with using stylesheets

    Hello,

    I have another question about stylesheet on complex widgets.
    Documentation contains following sentence:
    Note: With complex widgets such as QComboBox and QScrollBar, if one property or sub-control is customized, all the other properties or sub-controls must be customized as well.
    and I can't use custom border because buttons are disappearing.

    I would like to use standard images of arrows on buttons but without using 'image' property. Is it possible?
    Is it possible to draw QStyle::PE_IndicatorSpinUp with using stylesheet and how?


    Franko.

  2. #2
    Join Date
    Nov 2010
    Location
    Zawiercie, Poland
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSpinBox setting buttons with using stylesheets

    Maybe I didn't specify my problem. I would like to change size of the spinbox buttons. I do this with
    Qt Code:
    1. "QSpinBox::up-button { width: 32px; }"
    2. "QSpinBox::down-button { width: 32px; }"
    To copy to clipboard, switch view to plain text mode 
    and it works.
    Now I have a problem with background color.
    I want to change only background of spinbox area (for example yellow). When I try to do it with
    Qt Code:
    1. "QSpinBox { background-color: yellow; }"
    To copy to clipboard, switch view to plain text mode 
    all elements have yellow color.
    How can I prevent to change colors of buttons?

    Any help is appreciated

    Thank you.

  3. #3
    Join Date
    Nov 2010
    Location
    Zawiercie, Poland
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Lightbulb Re: QSpinBox setting buttons with using stylesheets

    OK. I found only this solution and I think I will use it:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. QCommonStyle style;
    11. ui->pushButtonBack->setIcon(style.standardIcon(QStyle::SP_ArrowBack));
    12. ui->pushButtonSend->setIcon(style.standardIcon(QStyle::SP_DialogOkButton));
    13.  
    14. QPalette palbut = ui->pushButtonBack->palette();
    15. QPalette palspin = ui->spinBox->palette();
    16. QPalette palyellow(QColor("yellow"));
    17.  
    18. // qDebug("height spinbox: %d", ui->spinBox->height());
    19.  
    20. ui->label->setStyleSheet("QLabel { border-color: yellow; border-style: inset; border-width: 5px; }");
    21.  
    22. ui->spinBox->setStyleSheet("QSpinBox { border: 3px solid red; border-radius: 5px; background-color: yellow; }"
    23. "QSpinBox::up-arrow { border-left: 17px solid none;"
    24. "border-right: 17px solid none; border-bottom: 17px solid black; width: 0px; height: 0px; }"
    25. "QSpinBox::up-arrow:hover { border-left: 17px solid none;"
    26. "border-right: 17px solid none; border-bottom: 17px solid black; width: 0px; height: 0px; }"
    27. "QSpinBox::up-button { width: 40px; height: 37px; background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.25 "+getRGBhexColor(palspin.light().color())+", stop: 1 "+getRGBhexColor(palspin.midlight().color())+") }"
    28. "QSpinBox::up-button:hover { width: 40px; height: 37px; background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.25 "+getRGBhexColor(palspin.light().color())+", stop: 1 "+getRGBhexColor(palspin.shadow().color())+") }"
    29.  
    30. "QSpinBox::down-arrow { border-left: 17px solid none;"
    31. "border-right: 17px solid none; border-top: 17px solid black; width: 0px; height: 0px; }"
    32. "QSpinBox::down-arrow:hover { border-left: 17px solid none;"
    33. "border-right: 17px solid none; border-top: 17px solid black; width: 0px; height: 0px; }"
    34. "QSpinBox::down-button { width: 40px; height: 37px; background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.25 "+getRGBhexColor(palspin.light().color())+", stop: 1 "+getRGBhexColor(palspin.midlight().color())+") }"
    35. "QSpinBox::down-button:hover { width: 40px; height: 37px; background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.25 "+getRGBhexColor(palspin.light().color())+", stop: 1 "+getRGBhexColor(palspin.shadow().color())+") }"
    36. );
    37.  
    38.  
    39. list.append("QPushButton { font: 75 28pt \"Serif\"; border-width: 3px; border-radius: 5px; border-style: solid; border-color: "+getRGBhexColor(palbut.mid().color())+"; "
    40. "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.5 "+getRGBhexColor(palbut.light().color())+", stop: 1 "+getRGBhexColor(palbut.dark().color())+") }"
    41. "QPushButton:pressed { background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 "+getRGBhexColor(palbut.dark().color())+", stop: 1 "+getRGBhexColor(palbut.light().color())+"); }");
    42. list.append("QPushButton { font: 75 28pt \"Serif\"; border: 1px solid "+getRGBhexColor(palyellow.shadow().color())+"; border-radius: 5px;"
    43. "background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0.5 "+getRGBhexColor(palyellow.button().color())+", stop: 1 "+getRGBhexColor(palyellow.shadow().color())+"); min-width: 10px; }"
    44. "QPushButton:pressed { background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 "+getRGBhexColor(palyellow.dark().color())+", stop: 1 "+getRGBhexColor(palyellow.light().color())+"); }"
    45. // "QPushButton:flat { border: none; }"
    46. // "QPushButton:default { border-color: navy; }"
    47. );
    48.  
    49. colorsStyleSheets.append(list);
    50. colorsStyleSheets.append(list);
    51.  
    52. connect(&colorTimer, SIGNAL(timeout()), this, SLOT(sColorTimerTimeout()));
    53. startColorTimer();
    54. }
    55.  
    56. MainWindow::~MainWindow()
    57. {
    58. delete ui;
    59. }
    60.  
    61. void MainWindow::changeEvent(QEvent *e)
    62. {
    63. QMainWindow::changeEvent(e);
    64. switch (e->type()) {
    65. case QEvent::LanguageChange:
    66. ui->retranslateUi(this);
    67. break;
    68. default:
    69. break;
    70. }
    71. }
    72.  
    73. void MainWindow::startColorTimer() {
    74. QWidget* twid = this->focusWidget();
    75. if(twid == ui->pushButtonBack)
    76. twid->setStyleSheet(colorsStyleSheets.at(0).at(1));
    77. else if(twid == ui->pushButtonSend)
    78. twid->setStyleSheet(colorsStyleSheets.at(1).at(1));
    79.  
    80. colorTimer.start(1000);
    81. }
    82.  
    83. void MainWindow::stopColorTimer() {
    84. colorTimer.stop();
    85. }
    86.  
    87. void MainWindow::sColorTimerTimeout() {
    88. QWidget* twid = this->focusWidget();
    89. QStringList tlist;
    90. int idx = -1;
    91. if(twid == ui->pushButtonBack)
    92. tlist = colorsStyleSheets.at(0);
    93. else if(twid == ui->pushButtonSend)
    94. tlist = colorsStyleSheets.at(1);
    95. if(twid == ui->pushButtonBack || twid == ui->pushButtonSend) {
    96. QPushButton* tpb = qobject_cast<QPushButton*>(twid);
    97. tpb->setFlat(false);
    98. idx = tlist.indexOf(twid->styleSheet());
    99. idx++;
    100. if(idx >= tlist.size())
    101. idx = 0;
    102. twid->setStyleSheet(tlist.at(idx));
    103. }
    104. }
    105.  
    106. QString MainWindow::getRGBhexColor(const QColor color) {
    107. QString col("#");
    108. // qDebug("red: %x", color.red());
    109. if(color.red() == 0)
    110. col.append("00");
    111. else
    112. col.append(QString::number(color.red(), 16));
    113. // qDebug("green: %x", color.green());
    114. if(color.green() == 0)
    115. col.append("00");
    116. else
    117. col.append(QString::number(color.green(), 16));
    118. // qDebug("blue: %x", color.blue());
    119. if(color.blue() == 0)
    120. col.append("00");
    121. else
    122. col.append(QString::number(color.blue(), 16));
    123. return col;
    124. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QStringList>
    6. #include <QTimer>
    7.  
    8. #include <QCommonStyle>
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow {
    15. Q_OBJECT
    16. public:
    17. MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. protected:
    21. void changeEvent(QEvent *e);
    22.  
    23. private slots:
    24. void sColorTimerTimeout();
    25.  
    26. private:
    27. void startColorTimer();
    28. void stopColorTimer();
    29.  
    30. QString getRGBhexColor(const QColor color);
    31.  
    32. Ui::MainWindow *ui;
    33.  
    34. QTimer colorTimer;
    35. QList<QStringList> colorsStyleSheets;
    36. };
    37.  
    38. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    But still this is not what i want to achive...

    When I use only this piece of code:
    Qt Code:
    1. ui->spinBox->setStyleSheet("background-color: yellow;")
    To copy to clipboard, switch view to plain text mode 
    I've got spinbox which looks like on image_1_background (white frame in text area with yellow background) which I would like to achive.
    But when I use code from above class I've got spinbox which looks like on image_2_whithout_frame (background color fills all content). I don't want have a color which fills background under buttons.

    If anyone have an idea how can I achieve my intended effect (from first image) with changing size of buttons I will be grateful.

    Thank you.
    Regards for programmers.
    Franko
    Attached Images Attached Images

Similar Threads

  1. Replies: 2
    Last Post: 16th June 2011, 11:56
  2. QSpinBox buttons size
    By bcastalia in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2011, 05:49
  3. Setting buttons on top...
    By Shiva in forum Qt Programming
    Replies: 3
    Last Post: 15th October 2010, 13:35
  4. Make QSpinBox up/down buttons disappear
    By stefanadelbert in forum Qt Programming
    Replies: 4
    Last Post: 15th April 2010, 00:40
  5. Programatically setting radio buttons
    By Doug Broadwell in forum Newbie
    Replies: 2
    Last Post: 25th May 2007, 21:55

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.