C++ C++;名称空间(选择器)
我想将我的全局C++ C++;名称空间(选择器),c++,qtstylesheets,qt5.3,C++,Qtstylesheets,Qt5.3,我想将我的全局qss样式表与派生类一起使用。我知道我必须覆盖绘画事件(,或) 然而,它似乎不起作用。使用CDerived:QWidget和我面对的以下样式表行: CDerived { background-color: black; } // no effect QWidget { background-color: black; } // works CDerived实现上述paintEvent。还有什么我需要做的吗 --编辑/解决方案-- 多亏了JK的暗示,我终于明白了。我上面的例子实际
qss
样式表与派生类一起使用。我知道我必须覆盖绘画事件(,或)
然而,它似乎不起作用。使用CDerived:QWidget
和我面对的以下样式表行:
CDerived { background-color: black; } // no effect
QWidget { background-color: black; } // works
CDerived
实现上述paintEvent
。还有什么我需要做的吗
--编辑/解决方案--
多亏了JK的暗示,我终于明白了。我上面的例子实际上并没有正确地反映我的情况<强>我的实类驻留在C++命名空间中(我的错误我错过了)。所以我必须在QSS中编写<代码> MyMaMeStay-cDebug <代码>。见“
在我尝试了JK的简单例子之后,我突然意识到我的错误
正确的一点:
MyNamespace--CDerived { background-color: black; } // works, use -- for ::
备注:与问题(,)相关,但未回答此特定问题。我的派生类位于C++命名空间中。p> 这很奇怪……对我来说效果很好:
untitled.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2014-10-07T11:34:54
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
mywidget.cpp
HEADERS += mainwindow.h \
mywidget.h
FORMS += mainwindow.ui
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="MyWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>70</x>
<y>30</y>
<width>201</width>
<height>121</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>MyWidget</class>
<extends>QWidget</extends>
<header>mywidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
主窗口
0
0
400
300
主窗口
70
30
201
121
30
20
75
23
按钮
0
0
400
21
顶部工具栏区域
假的
MyWidget
QWidget
mywidget.h
1.
mywidget.h:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *e);
};
#endif // MYWIDGET_H
\ifndef MYWIDGET\u H
#定义MYWIDGET_H
#包括
类MyWidget:publicqwidget
{
Q_对象
公众:
显式MyWidget(QWidget*parent=0);
受保护的:
无效油漆事件(QPaintEvent*e);
};
#endif//MYWIDGET_H
mywidget.cpp:
#include "mywidget.h"
#include <QStyleOption>
#include <QPainter>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
}
void MyWidget::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e)
QStyleOption opt;
opt.init(this); // tried initFrom too, same result=>not working
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
#包括“mywidget.h”
#包括
#包括
MyWidget::MyWidget(QWidget*父项):
QWidget(父级)
{
}
void MyWidget::paintEvent(QPaintEvent*e)
{
Q_(e)
qstyleopt;
opt.init(this);//也尝试了initFrom,相同的结果=>不工作
油漆工p(本);
style()->drawPrimitive(QStyle::PE_小部件,&opt,&p,this);
}
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyleSheet("MyWidget { background-color: red; }");
MainWindow w;
w.show();
return a.exec();
}
#包括“mainwindow.h”
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
a、 setStyleSheet(“MyWidget{背景色:红色;}”);
主窗口w;
w、 show();
返回a.exec();
}
我不知道是不是我,但我在这里找不到opt.init()
这里也可以看到使用Qt样式表的示例:是的,很奇怪,我正在测试Win 7,Qt 5.3。然而,你能做一个简短的交叉检查吗。如果“注释掉”MyWidget::paintEvent()函数,它会删除红色吗?今天晚些时候,我还将测试这样一个简单的例子,好主意。@HorstWalter是的,我试过了,如果你评论出了paintEvent,红色将被去除,多亏你的提示,我已经弄明白了。这是一个名称空间问题。我错过了a)我的类位于名称空间中,b)必须用“-”替换“:”。
#include "mywidget.h"
#include <QStyleOption>
#include <QPainter>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
}
void MyWidget::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e)
QStyleOption opt;
opt.init(this); // tried initFrom too, same result=>not working
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyleSheet("MyWidget { background-color: red; }");
MainWindow w;
w.show();
return a.exec();
}