PDA

View Full Version : How to use Q_PROPERTY in inheritance class ??



jslim
18th February 2019, 01:08
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]


#include "base_widget.h"
#include "loader_css.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString css = load_css( "css/menu.css" );
a.setStyleSheet( css );

base_widget w;
w.show();

return a.exec();
}


[base_widget.cpp, .h]


(base_widget.h)
#ifndef BASE_WIDGET_H
#define BASE_WIDGET_H

#include <QWidget>

class base_widget : public QWidget
{
Q_OBJECT

public:
base_widget(QWidget *parent = 0);
~base_widget();
};

#endif // BASE_WIDGET_H


(base_widget.cpp)
#include "base_widget.h"
#include "lv1_menu.h"

#define DEFUALT_LV1_MENU_INDEX HOME

lv1_menu * menuLv1;

base_widget::base_widget(QWidget *parent)
: QWidget(parent)
{
this->setWindowFlags( Qt::FramelessWindowHint );
this->setWindowState( Qt::WindowMaximized );

menuLv1 = new lv1_menu[MAX_LV1_LIST];
for( int i = 0 ; i < MAX_LV1_LIST ; ++i ) {
menuLv1[i].init( this, i );
if( i == DEFUALT_LV1_MENU_INDEX ) menuLv1[i].show();
else menuLv1[i].hide();
}
}

base_widget::~base_widget()
{

}


[lv1_menu.cpp, .h]


(lv1_menu.h)
#ifndef LV1_MENU_H
#define LV1_MENU_H

#include "menu_button.h"

enum lv1_menu_list {
HOME,
EVENT,
SETUP,
MAX_LV1_LIST
};

class lv1_menu : public menu_button
{
Q_PROPERTY( QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true )

public:
lv1_menu();
~lv1_menu();

void init( QWidget * parent, int menuIndex );
void paintEvent( QPaintEvent * e );

QColor getRectColor( );
void setRectColor( QColor c );

private:
QColor rectColor;
};

#endif // LV1_MENU_H

(lv1_menu.cpp)
#include "lv1_menu.h"
#include <QPainter>

struct lv1_menu_info {
int posLeft, posTop, width, height;
QString objectName;
QString title[MAX_LV1_LIST];
} lv1MenuInfo = {
0, 0, 100, 40,
"lv1Menu",
{ "HOME", "EVENT", "SETUP" }
};

lv1_menu::lv1_menu()
{

}

lv1_menu::~lv1_menu()
{

}

void lv1_menu::init( QWidget * parent, int menuIndex )
{
this->setParent( parent );
this->setObjectName(lv1MenuInfo.objectName);
}

void lv1_menu::paintEvent( QPaintEvent * e )
{
QPainter painter;
painter.begin( this );
painter.fillRect( lv1MenuInfo.posLeft, lv1MenuInfo.posTop, lv1MenuInfo.width, lv1MenuInfo.height, rectColor );
painter.end( );
}

QColor lv1_menu::getRectColor( )
{
printf("%s\n", __func__);
return rectColor;
}

void lv1_menu::setRectColor( QColor c )
{
printf("%s\n", __func__);
rectColor = c;
}


[css/menu.css]


lv1_menu#lv1Menu {
qproperty-rectColor: yellow;
}

anda_skoa
18th February 2019, 07:46
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,
_

jslim
19th February 2019, 00:16
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.

anda_skoa
19th February 2019, 13:59
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.



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



class lv1_menu : public menu_button
{
Q_OBJECT
Q_PROPERTY( QColor rectColor READ getRectColor WRITE setRectColor DESIGNABLE true )
Q_PROPERTY( bool bold READ getBold WRITE setBold DESIGNABLE true )
};


Cheers,
_

jslim
21st February 2019, 03:52
Thank anda_skoa :-) Q-Property works very well. good~