简体   繁体   中英

Unable to add styles to tab (bar) and tab (pane) in QT tab widget

I am adding Tabs to TabWidget dynamically from the constructor. I have been able to add the tab but have been unable to set the style for the Pane, Tab for the widget.

How do I add styles to tab (bar) and tab (pane)? The names of the tabs are dynamic and not static. Here is my code:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

{
    ui->setupUi(this);
    QPixmap httpPix(":/rec/img/http_2.png");
    ui->menuTabWidget->addTab(new Workspace(), QString("Projects"));
    ui->menuTabWidget->addTab(new HistoricalRequests(), QString("History"));
    ui->apiTabWidget->setStyleSheet("{background:#FFF}");
    //ui->apiTabWidget->setPalette(*(new QPalette(Qt::green)));
    //ui->apiTabWidget->tabBar()->setPalette(*(new QPalette(Qt::white)));
    ui->apiTabWidget->addTab(new APITab(), httpPix, QString("New Test 1"));
}

This is copied straight from production code style sheet that styles tabs. Unfortunately I can't share screenshots but it works most def!

Please note that MaintenanceBaseWActivity is the name of one window in the application so that these styles will only work on tabs in that window.

MaintenanceBaseWActivity .QTabBar::tab {
background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0.0 #666,
stop: 1.0 #aaa );
color: white;
padding: 0.4em;
margin:0;
height:1em;
min-width:5.7em;
max-width:5.7em;
width:5.7em;
border:0px;
border-top-left-radius:8px;
border-top-right-radius:8px;
}

MaintenanceBaseWActivity .QTabBar::tab:selected {
background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0.0 #bbb,
stop: 1.0 #ddd);
font-weight:bold;
color:black;
}

/*
MaintenanceBaseWActivity .QTabWidget::tab-bar {
width:1024px;
}
*/

MaintenanceBaseWActivity .QTabWidget::pane {
border-top: 8px solid #ddd;
}

EDIT: To answer your comment, I found that setting individual stylesheets per widget is not recommended. Better to name your widgets or at least style classes (you can use object names) in a central stylesheet and then apply that at startup.

Here is the exact code I use to apply the central stylesheet from a resource in my project:

QString fileToString(QString fn)
{
    QFile file(fn);
    if(file.open(QFile::ReadOnly)) {
        QTextStream in(&file);
        return in.readAll();
    }
    return "";
}


// Get the core app first
QCoreApplication *capp=static_cast<QCoreApplication *> QCoreApplication::instance());
// Then see if we can get a normal app (with UI and styles)
QApplication *app=qobject_cast<QApplication *>(capp);
if (nullptr!=app) {
    // I am using a resource, so I just make bloody sure it is loaded (since this code might run from palces in the program that might not have resources loaded yet.
    Q_INIT_RESOURCE(style);  //My resource file is called "style.qrc" so please adapt this to whatever you are using.
    // Time to load and set the stylesheet program wide. Please replace the path to the stylesheet you want, 
    app.setStyleSheet ( fileToString(":/style/style.qss"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM