我当前的项目中有几个自定义小部件.我希望将样式表应用到它们中,当我在Qt Creator中这样做时,它似乎有效.但是,在执行程序时,不使用样式表.Qt小部件的样式表正常工作.
有人有建议吗?这是一些相关的代码.
WidgetUnits.h
#ifndef WIDGETUNITS_H
#define WIDGETUNITS_H
#include <QList>
#include <QWidget>
#include <QPainter>
#include <Widgets/JECButton.h>
#include <Unit.h>
#include <Time.h>
namespace Ui
{
class WidgetUnits;
}
class WidgetUnits : public QWidget
{
Q_OBJECT
public:
explicit WidgetUnits(QWidget *parent = 0);
~WidgetUnits();
void setNumTimes(const int& numTimes);
public slots:
void updatePictures(const Time* time);
protected:
void paintEvent(QPaintEvent *event);
private:
void checkNewQueue(const QList<QList<Unit*>*>* units);
Ui::WidgetUnits *ui;
const int pictureWidth; // The width of the Unit pictures.
const int pictureHeight; // The height of the Unit …Run Code Online (Sandbox Code Playgroud) 我正在使用QPlainTextEdit和编写自定义代码编辑器,QSyntaxHighlighter我遇到了一个小故障.我想在选择中保留语法突出显示.但是,选择的颜色(环境颜色)会覆盖由QSyntaxHighlighterhtml标记突出显示的文本的颜色.保留字体系列等其他属性.
例:
没有选择:选择:

(我想Hello变成绿色,World!变成黑色)
我也尝试将样式表设置为:
QPlainTextEdit {
selection-color: rgba(0, 0, 0, 0);
selection-background-color: lightblue;
}
Run Code Online (Sandbox Code Playgroud)
结果:

背景颜色覆盖文本,并且文本颜色alpha = 0不可见.我这样做只是为了排除语法颜色持续存在的想法selection-color.事实上它被覆盖了selection-background-color.
编辑:不,如果我也设置selection-background-color为rgba(0, 0, 0, 0),则没有选择,并且该选择中没有文本.我所看到的只是背景.
使整个光标的线突出显示的以下片段的方法似乎是要走的路,但我基本上最终会重新实现所有的选择机制......
QList<QTextEdit::ExtraSelection> extraSelections;
QTextCursor cursor = textCursor();
QTextEdit::ExtraSelection selection;
selection.format.setBackground(lineHighlightColor_);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = cursor;
selection.cursor.clearSelection();
extraSelections.append(selection);
setExtraSelections(extraSelections);
Run Code Online (Sandbox Code Playgroud)
有没有更简单的解决方案?
我希望能够使用样式表设置QGroupBox标题的字体大小.我似乎无法弄明白.
根据我在这里读到的内容,我提出了以下代码.不幸的是,它不太有用.
groupbox->setStyleSheet(style)
Run Code Online (Sandbox Code Playgroud)
在哪里style:
QGroupBox::title
{
subcontrol-origin: margin;
subcontrol-position: top left;
padding: 5 5px;
font-size: 18px;
font-weight: bold;
}
Run Code Online (Sandbox Code Playgroud)
所有这些风格元素似乎都被尊重,除了font-size和font-weight.根据Qt Style Sheets Reference,所有尊重QWidget :: font的小部件都支持字体"属性".这不是QGroupBox标题的情况吗?
我正在使用以下代码连接QMenu到QPushButton.单击按钮时,会显示包含多个子菜单项的下拉菜单.
button=QPushButton()
button.setText("Press Me")
font=QtGui.QFont()
button.setFont(font)
button.setSizePolicy(ToolButtonSizePolicy)
button.setPopupMode(QtGui.QToolButton.InstantPopup)
menu=QtGui.QMenu()
button.setMenu(menu)
menuItem1=menu.addAction('Menu Item1')
menuItem2=menu.addAction('Menu Item2')
Run Code Online (Sandbox Code Playgroud)
现在根据条件,我想QPushButton通过给它一个文本和背景颜色自定义显示.以下代码行(应该更改背景颜色)对QPushButton连接到QMenu 没有影响.
button.setStyleSheet('QPushButton {background-color: #A3C1DA}')
Run Code Online (Sandbox Code Playgroud)
我想知道如何更改QPushButton按钮的文本颜色的背景颜色.
我正在OSX Mavericks上编写新手Qt5代码,并且想要覆盖给定小部件的StyleSheet的一个属性:值对.
如果我运行以下自包含的演示代码:
#include <QApplication>
#include <QMainWindow>
#include <QtGui>
#include <QPushButton>
int
main( int argc, char *argv[] ) {
QApplication app( argc, argv );
QMainWindow* mw = new QMainWindow();
QPushButton* AButton = new QPushButton( "A Button", mw );
mw->show();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个很好的按钮,默认为Macintosh风格 - 圆角,按下时标准OSX-蓝色等:

但是,如果我设置样式表以使背景按钮颜色为红色,那么在此过程中我似乎失去了所有其他OSX样式默认值 - 没有更多的圆角,标准边距等:
#include <QApplication>
#include <QMainWindow>
#include <QtGui>
#include <QPushButton>
int
main( int argc, char *argv[] ) {
QApplication app( argc, argv );
QMainWindow* mw = new QMainWindow();
QPushButton* AButton = new QPushButton( "A …Run Code Online (Sandbox Code Playgroud) 是否有可能在.qss文件中为hex/rgb数字赋予变量名称.对呃
myColor = #FFCC08
QPushButton { background-color: myColor;}
Run Code Online (Sandbox Code Playgroud)
这样我就可以在样式表的顶部定义变量,并使用变量名称wheverver required而不是使用十六进制代码.此外,如果我需要更改颜色,那么我必须在一个地方更改,它将反映在文件中.
我也搜索了萨斯,但不知道如何在qt中使用它.
谢谢 :)
这可能是一个简单的问题,但我试图在我的应用程序中为特定的QLabel赋予颜色,但它不起作用.
我尝试的代码如下:
nom_plan_label = QtGui.QLabel()
nom_plan_label.setText(nom_plan_vignette)
nom_plan_label.setStyleSheet("QLabel#nom_plan_label {color: yellow}")
Run Code Online (Sandbox Code Playgroud)
任何提示都将不胜感激
如何隐藏QScrollBar箭头?
我需要隐藏在水平滚动条中.我试图隐藏setStyleSheet:
setStyleSheet(" QScrollBar:left-arrow:horizontal, QScrollBar::right-arrow:horizontal { height:0px; }" )
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我正在使用Qt,我在Qt Designer编辑器中设置了QTabWidget,你可以在图1中看到它.
图1 http://i40.tinypic.com/1109ba1.jpg
正如你在Tab4之后看到的那样,一直有一个空的空间一直到右边缘,在某种程度上我需要用一种颜色填充那个空间,如图2所示(最好的是能够设置褪色的颜色) .或者另一种解决方案是标签浮出来覆盖整个屏幕.
图2 http://i41.tinypic.com/zwmijr.jpg
我现在使用以下样式表:
QTabWidget::tab-bar {
}
QTabBar::tab {
background: gray;
color: white;
padding: 10px;
}
QTabBar::tab:selected {
background: lightgray;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用Qt样式表设置QTabBar的背景颜色?或者我可以使用Qt样式表将标签浮动到边缘?
编辑:我一直在尝试Caleb Huitt - cjhuitt在下面提出的解决方案.我真的很喜欢让标签扩展但无法使其正常工作的想法.
在Qt Designer Editor中,我右键单击我的QTabWidget - >"Promote To ..."并选择"Base class name":QTabWidget"Promoted class name":ExpandableTabWidget然后我单击add然后单击Promote.
在我设置的包含我的QTabWidget的小部件的init方法中
ui.tabWidget->SetTabsExpanding(true);
Run Code Online (Sandbox Code Playgroud)
一切都很好,但QTabbar没有扩展.
难道我做错了什么?
谢谢!
我正在制作一个字典程序,在3列子QTableView类中显示单词定义,当用户键入它们时,从QAbstractTableModel子类中获取数据.像这样的东西:
我想在文本中添加各种格式,我用来在数据进入时向每个单元格QAbstractItemView::setIndexWidget添加一个QLabel:
WordView.h
#include <QTableView>
class QLabel;
class WordView : public QTableView {
Q_OBJECT
public:
explicit WordView(QWidget *parent = 0);
void rowsInserted(const QModelIndex &parent, int start, int end);
private:
void insertLabels(int row);
void removeLabels(int row);
};
Run Code Online (Sandbox Code Playgroud)
WordView.cpp
#include <QLabel>
#include "WordView.h"
WordView::WordView(QWidget *parent) :
QTableView(parent)
{}
void WordView::rowsInserted(const QModelIndex &parent, int start, int end) {
QTableView::rowsInserted(parent, start, end);
for (int row = start; row <= end; ++row) {
insertLabels(row);
}
}
void …Run Code Online (Sandbox Code Playgroud) qtstylesheets ×10
qt ×9
qt5 ×3
pyqt ×2
python ×2
c++ ×1
pyqt4 ×1
qlabel ×1
qpushbutton ×1
qtableview ×1
qtabwidget ×1
scrollbar ×1
stylesheet ×1