V
V
Vlad2016-12-23 14:36:58
Qt
Vlad, 2016-12-23 14:36:58

How to include a qss stylesheet?

Hello everyone! With coming!
I'm trying to make a single stylesheet for a QT 5.7 Widgets program.
I do according to the instructions: Link
I have a QTabBar. The object name is myTabWidget.
I'm trying to do like this:

QFile styleFile(":/qss/style.qss");
styleFile.open(QFile::ReadOnly);
QString styleSheet = QString::fromLatin1( styleFile.readAll() );
styleFile.close();

ui->myTabWidget->setStyleSheet(styleSheet);

But nothing happens. I checked the styleSheet variable, the styles from the file are pulled up.
The strange thing is that if I connect the same line from the file directly:
ui->myTabWidget->setStyleSheet("QTabBar::tab:first { border-top: none; }");

That all works.
Help me please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Michael., 2016-12-23
@Result007

I did this (it really was a long time ago, maybe it needs to be corrected in the current version of Qt):

int main(int argc, char** argv)
{	
  QApplication app(argc, argv);
  QFile file(":/qss/stylesheet.qss");
  file.open(QFile::ReadOnly);
  app.setStyleSheet(file.readAll());
  bool ok = app.exec(); 
  return ok;
}

in the stylesheet.qss file:
QTabWidget {
  background-color: #999999;
}

A
akzhan, 2016-12-23
@akzhan

Вы уверены, что кодировка таблицы стилей latin1?
Я бы использовал QString::fromUtf8.
Закрывать файл, насколько я понимаю, не надо.

Яков Е, 2016-12-24
@Zifix Куратор тега Qt

a) Custom interfaces are better done on QML, widgets will not receive further development, no one will fix bugs with QSS, etc.
b) https://habrahabr.ru/company/istodo/blog/216275/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question