Hi all, I have a question and hope your good reply.

Please understand my weak English.

I want to use css StyleSheet to "paintEvent" function by Q_PROPERTY. And I think I have done everything.

But css was not applied. Let me know how can apply css.

I use Qt-4.7.4 C++ language and my target is embedded, Bellow my codes.

[main.cpp]
Qt Code:
  1. #include "base_widget.h"
  2. #include "loader_css.h"
  3. #include <QApplication>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8. QString css = load_css( "css/menu.css" );
  9. a.setStyleSheet( css );
  10.  
  11. base_widget w;
  12. w.show();
  13.  
  14. return a.exec();
  15. }
To copy to clipboard, switch view to plain text mode 

[base_widget.cpp, .h]
Qt Code:
  1. (base_widget.h)
  2. #ifndef BASE_WIDGET_H
  3. #define BASE_WIDGET_H
  4.  
  5. #include <QWidget>
  6.  
  7. class base_widget : public QWidget
  8. {
  9. Q_OBJECT
  10.  
  11. public:
  12. base_widget(QWidget *parent = 0);
  13. ~base_widget();
  14. };
  15.  
  16. #endif // BASE_WIDGET_H
  17.  
  18.  
  19. (base_widget.cpp)
  20. #include "base_widget.h"
  21. #include "lv1_menu.h"
  22.  
  23. #define DEFUALT_LV1_MENU_INDEX HOME
  24.  
  25. lv1_menu * menuLv1;
  26.  
  27. base_widget::base_widget(QWidget *parent)
  28. : QWidget(parent)
  29. {
  30. this->setWindowFlags( Qt::FramelessWindowHint );
  31. this->setWindowState( Qt::WindowMaximized );
  32.  
  33. menuLv1 = new lv1_menu[MAX_LV1_LIST];
  34. for( int i = 0 ; i < MAX_LV1_LIST ; ++i ) {
  35. menuLv1[i].init( this, i );
  36. if( i == DEFUALT_LV1_MENU_INDEX ) menuLv1[i].show();
  37. else menuLv1[i].hide();
  38. }
  39. }
  40.  
  41. base_widget::~base_widget()
  42. {
  43.  
  44. }
To copy to clipboard, switch view to plain text mode 

[lv1_menu.cpp, .h]
Qt Code:
  1. (lv1_menu.h)
  2. #ifndef LV1_MENU_H
  3. #define LV1_MENU_H
  4.  
  5. #include "menu_button.h"
  6.  
  7. enum lv1_menu_list {
  8. HOME,
  9. EVENT,
  10. SETUP,
  11. MAX_LV1_LIST
  12. };
  13.  
  14. class lv1_menu : public menu_button
  15. {
  16. Q_PROPERTY( QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true )
  17.  
  18. public:
  19. lv1_menu();
  20. ~lv1_menu();
  21.  
  22. void init( QWidget * parent, int menuIndex );
  23. void paintEvent( QPaintEvent * e );
  24.  
  25. QColor getRectColor( );
  26. void setRectColor( QColor c );
  27.  
  28. private:
  29. QColor rectColor;
  30. };
  31.  
  32. #endif // LV1_MENU_H
  33.  
  34. (lv1_menu.cpp)
  35. #include "lv1_menu.h"
  36. #include <QPainter>
  37.  
  38. struct lv1_menu_info {
  39. int posLeft, posTop, width, height;
  40. QString objectName;
  41. QString title[MAX_LV1_LIST];
  42. } lv1MenuInfo = {
  43. 0, 0, 100, 40,
  44. "lv1Menu",
  45. { "HOME", "EVENT", "SETUP" }
  46. };
  47.  
  48. lv1_menu::lv1_menu()
  49. {
  50.  
  51. }
  52.  
  53. lv1_menu::~lv1_menu()
  54. {
  55.  
  56. }
  57.  
  58. void lv1_menu::init( QWidget * parent, int menuIndex )
  59. {
  60. this->setParent( parent );
  61. this->setObjectName(lv1MenuInfo.objectName);
  62. }
  63.  
  64. void lv1_menu::paintEvent( QPaintEvent * e )
  65. {
  66. QPainter painter;
  67. painter.begin( this );
  68. painter.fillRect( lv1MenuInfo.posLeft, lv1MenuInfo.posTop, lv1MenuInfo.width, lv1MenuInfo.height, rectColor );
  69. painter.end( );
  70. }
  71.  
  72. QColor lv1_menu::getRectColor( )
  73. {
  74. printf("%s\n", __func__);
  75. return rectColor;
  76. }
  77.  
  78. void lv1_menu::setRectColor( QColor c )
  79. {
  80. printf("%s\n", __func__);
  81. rectColor = c;
  82. }
To copy to clipboard, switch view to plain text mode 

[css/menu.css]
Qt Code:
  1. lv1_menu#lv1Menu {
  2. qproperty-rectColor: yellow;
  3. }
To copy to clipboard, switch view to plain text mode