关于Qt的QMainWindow、QTableWidget、QComboBox、QScrollArea的widget导致的qss无效问题

31 篇文章 2 订阅
12 篇文章 11 订阅

问题

前阵子比较忙,忘记记录这个问题了。事情是我在写QTableWidget的qss时候,要把下图红色箭头指向的这个边角样式改变时发现的。我发现我对QTabelCornerButton写样式无效。但是我印象里,很早之前也对这个东西写过qss,我记得是生效的,所以花了点心思研究了下。

图1-1

发现

煞费心思找到了之前的代码,比对了下并无不同,但是一个qss生效一个无效。所以感觉是QTabelWidget出的问题。以前的QTableWidget是UI Designer直接拉的,而现在的是new出来的。不禁回想起以前的两个坑。
QScrollArea问题以及QMainWindow的qss问题

分析

Qt里面new的东西和Designer拉的东西是不一样的。拿MainWindow来说,不是设计界面拉出来的是没有centralWidget的。
在这里插入图片描述
QScrollArea也是一样,代码生成的也是没有核心的Widget的。QTableWidget也是一样,没有CornerWidget,自然qss无法生效。
所以需要自己手动添加。

pTable->setCornerWidget(new QWidget());

一样的道理,QComboBox要设置下拉框qss的话,是对QAbstractItemView进行设置。比如设置下拉框高度。

QComboBox QAbstractItemView::item{height:40px;}

但是如果不是设计器拉出来,这样写是无效的。因为代码生成的QComboBox里面压根没有view。所以需要自己添加。

pComboBox->setView(new QListView());

其它的Qt控件应该也是一样的道理。

其它方法

当然,上面的QTableWidget不设置Corner也是可以的,之前没发现问题时候,用qApp加载解决了。

qApp->setStyleSheet(QStringLiteral("QTableCornerButton::section{XXX};

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下代码在Qt Creator 6.0中向QScrollArea中添加另一个QScrollArea: ```cpp #include <QtWidgets> int main(int argc, char *argv[]) { QApplication app(argc, argv); // 创建主窗口 QMainWindow mainWindow; QWidget* centralWidget = new QWidget(&mainWindow); QVBoxLayout* mainLayout = new QVBoxLayout(centralWidget); // 创建第一个QScrollArea QScrollArea* scrollArea1 = new QScrollArea(&mainWindow); QWidget* scrollContent1 = new QWidget(scrollArea1); QVBoxLayout* scrollLayout1 = new QVBoxLayout(scrollContent1); // 添加内容到第一个QScrollArea QLabel* label1 = new QLabel("Content 1"); scrollLayout1->addWidget(label1); // 设置一个QScrollArea的滚动内容 scrollContent1->setLayout(scrollLayout1); scrollArea1->setWidget(scrollContent1); // 创建第二个QScrollArea QScrollArea* scrollArea2 = new QScrollArea(&mainWindow); QWidget* scrollContent2 = new QWidget(scrollArea2); QVBoxLayout* scrollLayout2 = new QVBoxLayout(scrollContent2); // 添加内容到第二个QScrollArea QLabel* label2 = new QLabel("Content 2"); scrollLayout2->addWidget(label2); // 设置第二个QScrollArea的滚动内容 scrollContent2->setLayout(scrollLayout2); scrollArea2->setWidget(scrollContent2); // 添加两个QScrollArea到主布局 mainLayout->addWidget(scrollArea1); mainLayout->addWidget(scrollArea2); centralWidget->setLayout(mainLayout); mainWindow.setCentralWidget(centralWidget); mainWindow.show(); return app.exec(); } ``` 这段代码创建了一个主窗口,并在其中添加了两个QScrollArea。每个QScrollArea都包含一个QLabel作为滚动内容。您可以根据需要修改和扩展这个示例。记得在Qt Creator 6.0中构建和运行该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值