У меня есть проблема, пока я пытался применить таблицу стилей QSS для моего qtreewidget в моем проекте QT. Когда я нажимаю на элемент, значок элемента внутри qtreewidget меняю цвет и становится синим.#ifndef FILESIDEBARWIDGET_H
#define FILESIDEBARWIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
class CustomFileSystemModel : public QFileSystemModel {
Q_OBJECT
public:
explicit CustomFileSystemModel(QObject *parent = nullptr) : QFileSystemModel(parent) {}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
if (role == Qt::DecorationRole && index.column() == 0) {
QFileInfo info = fileInfo(index);
QIcon icon;
if (info.isDir()) {
icon = QIcon(":/images/icons/directory.png");
} else if (info.isFile()) {
QString name = info.fileName();
if (hasMatchingExtension(name, QStringList{"png", "jpg", "jpeg", "gif", "bmp"})) {
icon = QIcon(":/images/icons/fileImg.png");
} else if (hasMatchingExtension(name, QStringList{"bin", "o", "exe", "dll", "so"})) {
icon = QIcon(":/images/icons/fileBin.png");
} else {
icon = QIcon(":/images/icons/fileText.png");
}
}
return icon.pixmap(16, 16);
}
return QFileSystemModel::data(index, role);
}
private:
bool hasMatchingExtension(const QString &fileName, const QStringList &extensions) const {
QString lowerFileName = fileName.toLower();
for (const QString &ext : extensions) {
QString lowerExt = ext.toLower();
if (lowerFileName.endsWith("." + lowerExt)) {
return true;
} else if (lowerFileName.contains("." + lowerExt)) {
return true;
}
}
return false;
}
};
class FileSidebarWidget : public QWidget {
Q_OBJECT
public:
explicit FileSidebarWidget(QWidget *parent = nullptr) : QWidget(parent) {
fileModel = new CustomFileSystemModel(this);
fileModel->setRootPath(QDir::currentPath());
fileModel->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
treeView = new QTreeView(this);
treeView->setModel(fileModel);
treeView->setRootIndex(fileModel->index(QDir::currentPath()));
treeView->setHeaderHidden(true);
treeView->setColumnHidden(1, true);
treeView->setColumnHidden(2, true);
treeView->setColumnHidden(3, true);
treeView->setAnimated(false);
treeView->setIndentation(20);
treeView->setSortingEnabled(false);
treeView->setFocusPolicy(Qt::NoFocus);
treeView->setStyleSheet(R"(
QTreeView {
background-color: #21252b;
border: none;
color: #d3d3d3;
}
QTreeView::item {
padding: 2px;
color: #d3d3d3;
border: none;
outline: none;
}
QTreeView::item:selected {
background-color: #4d78cc;
color: white;
}
)");
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(treeView);
setLayout(layout);
connect(treeView, &QTreeView::doubleClicked, this, &FileSidebarWidget::onFileSelected);
}
signals:
void fileSelected(const QString &filePath);
private slots:
void onFileSelected(const QModelIndex &index) {
QString path = fileModel->filePath(index);
if (QFileInfo(path).isFile()) emit fileSelected(path);
}
private:
QTreeView *treeView;
CustomFileSystemModel *fileModel;
};
#endif // FILESIDEBARWIDGET_H
< /code>
Вот минимальный воспроизводимый пример, который показывает, что даже с отключенным фокусом значок все еще появляется с синим оттенком, когда вы нажимаете на него: < /p>
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QFileSystemModel *model = new QFileSystemModel;
model->setRootPath(QDir::currentPath());
model->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
QTreeView *view = new QTreeView;
view->setModel(model);
view->setRootIndex(model->index(QDir::currentPath()));
view->setHeaderHidden(true);
view->setFocusPolicy(Qt::NoFocus);
view->setStyleSheet(R"(
QTreeView {
background-color: #2b2b2b;
color: #dcdcdc;
border: none;
}
QTreeView::item:selected {
background: transparent;
color: #dcdcdc;
}
QTreeView::item:focus {
outline: none;
}
)");
view->resize(600, 400);
view->show();
return app.exec();
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... s-on-icons
QT Styling Qtreewidget: Удалите фокус на значках ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение