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[])
{
QString css
= load_css
( "css/menu.css" );
a.setStyleSheet( css );
base_widget w;
w.show();
return a.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
[base_widget.cpp, .h]
(base_widget.h)
#ifndef BASE_WIDGET_H
#define BASE_WIDGET_H
#include <QWidget>
{
Q_OBJECT
public:
~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
){
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()
{
}
(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()
{
}
To copy to clipboard, switch view to plain text mode
[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 setRectColor
( QColor c
);
private:
};
#endif // LV1_MENU_H
(lv1_menu.cpp)
#include "lv1_menu.h"
#include <QPainter>
struct lv1_menu_info {
int posLeft, posTop, width, height;
} 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);
}
{
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;
}
(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;
}
To copy to clipboard, switch view to plain text mode
[css/menu.css]
lv1_menu#lv1Menu {
qproperty-rectColor: yellow;
}
lv1_menu#lv1Menu {
qproperty-rectColor: yellow;
}
To copy to clipboard, switch view to plain text mode
Bookmarks