Results 1 to 5 of 5

Thread: How to use Q_PROPERTY in inheritance class ??

  1. #1
    Join Date
    Feb 2019
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to use Q_PROPERTY in inheritance class ??

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,292
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use Q_PROPERTY in inheritance class ??

    Your lv1_menu class is missing the Q_OBJECT macro.

    Unrelated but also: you are leaking base_widget::menuLv1. I would suggest using a QVector instead of an array

    Cheers,
    _

  3. #3
    Join Date
    Feb 2019
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use Q_PROPERTY in inheritance class ??

    Quote Originally Posted by anda_skoa View Post
    Your lv1_menu class is missing the Q_OBJECT macro.

    Unrelated but also: you are leaking base_widget::menuLv1. I would suggest using a QVector instead of an array

    Cheers,
    _
    I really appreciate it. You are a genius. Thank you :-)
    Can I ask you one more thing??

    Can you tell me how to set up BORDER PIXEL, BOLD using Q_PROPERTY?

    Thank you all the time.
    Last edited by jslim; 19th February 2019 at 00:17. Reason: updated contents

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,292
    Thanks
    36
    Thanked 1,521 Times in 1,472 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use Q_PROPERTY in inheritance class ??

    Quote Originally Posted by jslim View Post
    I really appreciate it. You are a genius. Thank you :-)
    You're welcome :-)

    Took me actually quite a while to see it, everything looked correct at first.

    Quote Originally Posted by jslim View Post
    Can you tell me how to set up BORDER PIXEL, BOLD using Q_PROPERTY?
    Not sure what you mean but you can just add more Q_PROPERTY declarations to the same class

    Qt Code:
    1. class lv1_menu : public menu_button
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY( QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true )
    5. Q_PROPERTY( bool bold READ getBold WRITE setBold DESIGNABLE true )
    6. };
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. #5
    Join Date
    Feb 2019
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use Q_PROPERTY in inheritance class ??

    Thank anda_skoa :-) Q-Property works very well. good~

Similar Threads

  1. Serialize nested user defined class in Q_PROPERTY
    By EagleNN in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2017, 16:11
  2. Qt Creator new class wizard vs. class inheritance
    By googie in forum Qt Tools
    Replies: 5
    Last Post: 4th March 2013, 17:52
  3. Replies: 2
    Last Post: 23rd October 2012, 09:46
  4. Inheritance of nested class
    By bibhukalyana in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2011, 18:54

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.