Qt QFrame StyleSheet 在 MainWindow 中不起作用
Qt QFrame StyleSheet not working in MainWindow
我正在为一个基本的样式表问题而苦苦挣扎。 我正在尝试将样式表应用于主窗口中的 QFrame,但由于某种原因,样式表被忽略了。 这似乎与自定义小部件的Qt样式表和样式表的启用自己的小部件有关,但是我尝试添加paintEvent,但没有帮助。 它一定是简单的东西,但我看不到它。 谁能帮忙?
主.cpp:
#include <QApplication>
#include <QDebug>
#include "mainwindow.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindowDef mainWindow;
mainWindow.show();
return app.exec();
}
主窗口.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets>
class MainWindowDef : public QMainWindow {
Q_OBJECT
public:
MainWindowDef(QWidget *parent = 0);
protected:
void paintEvent(QPaintEvent *);
};
#endif
主窗口.cpp:
#include <QDebug>
#include "mainwindow.h"
MainWindowDef::MainWindowDef(QWidget *parent) : QMainWindow(parent) {
QFrame *frame = new QFrame;
QVBoxLayout *layout = new QVBoxLayout;
QPushButton *button = new QPushButton("Press");
layout->addWidget(button);
QString myStyle = QString( "QFrame {"
"border: 2px solid green;"
"border-radius: 4px;"
"padding: 2px"
"background-color: red;"
"color: blue;"
"}");
frame->setStyleSheet(myStyle);
frame->setLayout(layout);
frame->setAutoFillBackground(true);
setCentralWidget(frame);
}
void MainWindowDef::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
我终于想通了。 它缺少一个分号。
"padding: 2px"
应该说:
"padding: 2px;"
叹息。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- Qt QFrame StyleSheet 在 MainWindow 中不起作用