How to get usesScrollButtons buttons to scroll tabs when it has many tabs For MAC?



  • Hello,
    I am using QTabBar in my application. I would use the QTabBar::usesScrollButtons for many tab. I am getting usesScrollButtons in windows OS.

    take screenshot from Windows OS.

    0_1536873363713_7a913be4-b3f1-489b-925a-ffc20caa6a3a-image.png

    But Whenever I tried same code in MAC system. At that time I am getting like below pic.

    0_1536873299425_67113e52-eddf-430f-85de-a6a34ead75cd-image.png

    I tried setUsesScrollButtons(true) but i did not get any change in Mac System.

    am I missing any step for MAC system?

    MinimumWidthTabBar .h file

    class MinimumWidthTabBar : public QTabBar {
    public:
    	MinimumWidthTabBar(QWidget *parent = 0);
    
    	QSize minimumSizeHint() const Q_DECL_OVERRIDE;
    };
    

    MinimumWidthTabBar.cpp file

    #include "MinimumWidthTabBar.h"
    
    MinimumWidthTabBar::MinimumWidthTabBar(QWidget *parent) : 
    	QTabBar(parent)
    {
    }
    QSize MinimumWidthTabBar::minimumSizeHint() const {
    	return sizeHint();
    }
    

    use MinimumWidthTabBar in function

    static QWidget *w = 0;
    	
    	if (w) {
    		return w;
    	}
    
    	// create New Object and give a name "new-data-window-owner"
    	w = OBJ_NAME(WDG(), "new-data-window-owner");
    	
    	// create vertical layout
    	auto *lay = NO_SPACING(NO_MARGIN(new QVBoxLayout(w)));
    	
    	// create pointer for Pushbutton "+" and Tile button
    	QPushButton *addNewButton = nullptr;
    	QPushButton *tileButton = nullptr;
    	
    	QTabBar *tabBar = nullptr;
    
    	
    	auto stackedLayWdg = OBJ_NAME(WDG(), "new-data-window-placeholder");
    	
    
    	auto stackedLayWdgLay = NO_SPACING(NO_MARGIN(new QHBoxLayout(stackedLayWdg)));
    	auto mdiArea = new QMdiArea;
    	
    	mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    	mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    	
    	m_ui.newDataTab.mdiArea = mdiArea;
    	stackedLayWdgLay->addWidget(mdiArea);
    	
    	//create new frame for data window 
    	QFrame *tabFrame = OBJ_NAME(new QFrame, "builder-tab-frame");
    	auto *tabFrameLay = NO_SPACING(NO_MARGIN(new QHBoxLayout(tabFrame)));
    	
    
    	// create tab into tab bar
    	tabFrameLay->addWidget(tabBar = OBJ_NAME(new MinimumWidthTabBar, "new-data-window-tab"));
    	tabFrameLay->addWidget(addNewButton = OBJ_NAME(PBT("+"), "builder-tab-add-new"));
    	tabFrameLay->addStretch(1);
    	tabFrameLay->addWidget(tileButton = OBJ_NAME(PBT("View All Graphs"), "builder-tab-mdi"));
    
    	m_ui.newDataTab.tabBar = tabBar;
    	
    	lay->addWidget(tabFrame);
    	lay->addWidget(stackedLayWdg);
    	
    
    	tabBar->setExpanding(false);
    	tabBar->setMovable(true);
    
    
    auto UpdateTabAndCustomFrame = [=](const QUuid id, ExperimentType type, QWidget* customFrame, const QString tabName, const int tabIndex, bool isExperimentRun) {
    
    		tabBar->insertTab(tabIndex, tabName);
    		tabBar->setCurrentIndex(tabIndex);
    		
    	};
    
    m_connections << QObject::connect(addNewButton, &QPushButton::clicked, [=]() {
    		MainWindow::CsvFileData csvData;
    		if (!m_mainWindow->ReadCsvFile(m_mainWindow, csvData)) {
    			return;
    		}
    		
    		// when we Pressed "+" button at that time tab will be shown into tabHeaderLay
    		if (tabBar->isHidden()) {
    			tabBar->show();
    		}
    
    		// print the name into tab bar
    		QString tabName = csvData.fileName;
    		
    		// craete Uniq ID for for Window
    		const QUuid id = QUuid::createUuid();
    
    		auto dataTabWidget = CreateNewDataTabWidget(id,
    			ET_SAVED,
    			tabName,
    			csvData.xAxisList,
    			csvData.yAxisList,
    			csvData.filePath,
    			&csvData.container);
    
    		UpdateTabAndCustomFrame(id, ET_SAVED, dataTabWidget, tabName, tabBar->count(), false);
    
    	
    	});
    
    

  • Lifetime Qt Champion

    Hi,

    What version of Qt are you using ?
    On what version of macOS ?

    Can you provide a minimal compilable example that shows that behaviour ?


  • Moderators

    I think that this is because the style does not support it, so if you are using the macOS style you won't get the buttons. Try running your application with -style fusion and then it should show up.



  • @SGaist Thank you for taking interest in my question.

    I am using Qt 5.11.0 and MacOS version is 10.13.5.

    Here I put separate function using QTabBar.

    QWidget* TestClass::getwidget(){
        static QWidget *w = 0;
        
        if (w) {
            return w;
        }
        
        w = new QWidget();
        
        auto *lay = (new QVBoxLayout(w));
        lay->setContentsMargins(0,0,0,0);
        lay->setSpacing(0);
        
        QPushButton *addNewButton = nullptr;
        QPushButton *tileButton = nullptr;
        
        QTabBar *tabBar = nullptr;
        
        
        QFrame *tabFrame = new QFrame();
        auto *tabFrameLay = new QHBoxLayout(tabFrame);
        tabFrameLay->setContentsMargins(0,0,0,0);
        tabFrameLay->setSpacing(0);
        
        tabFrameLay->addWidget(tabBar = new QTabBar);
        tabFrameLay->addWidget(addNewButton = new QPushButton("+"));
        tabFrameLay->addStretch(1);
        tabFrameLay->addWidget(tileButton = new QPushButton("View All Graphs"));
        lay->addWidget(tabFrame);
        
        tabBar->setExpanding(false);
        tabBar->setMovable(true);
        //tabBar->setUsesScrollButtons(true);
        
        static int tabIndex = 0;
        QObject::connect(addNewButton, &QPushButton::clicked, [=]() {
            tabBar->insertTab(tabIndex, "---------------TabName----123456789");
            tabBar->setCurrentIndex(tabIndex);
            tabIndex++;
        });
        
        return w;
    }
    

    Pic from windows
    0_1536877801508_9dc39185-701b-41c0-8553-04fc8568bf30-image.png

    pic from MAC
    0_1536877944601_70dc5d51-7e1a-4d60-b456-5e383a297102-image.png



  • @AndyS Thank you for direction. I am sorry, May be it is silly question. But I don't about -style fusion. What is -style fusion? How can I use it?


  • Moderators

    @Yash001 I suggest you have a look at http://doc.qt.io/qt-5/qstyle.html as this will give more details. But the quick version is to just start your application with the "-style fusion" arguments.



  • @AndyS Thank you I will go in more detail.



  • Hi @AndyS,

    I am still stuck in same issue, as per your guideline, and from reading documents.

    The style of the entire application can be set using the QApplication::setStyle() function. It can also be specified by the user of the application, using the -style command-line option:

    I create my own style for QToolButton with help GUI.css file, and I applied to application with help of ApplyStyle() function.

    I am calling ApplyStyle() function is from constructor.

    ApplyStyle() function definition.

    void MainWindow::ApplyStyle() {
    
        QFile f("./GUI.css");
    
        if(!f.open(QIODevice::ReadOnly))
            return;
    
       QString newStyleSheet = f.readAll();
    	
        qobject_cast<QApplication *>(QApplication::instance())->setStyleSheet(newStyleSheet);
    
        f.close();
    }
    

    GUI.css file.

    QTabBar QToolButton { /* the scroll buttons are tool buttons */
        background:light yellow;
    }
    
    QTabBar QToolButton::right-arrow { /* the arrow mark in the tool buttons */
    border:5px solid red;
    }
    
    QTabBar QToolButton::left-arrow {
    border:5px solid green;
    }
    

    Still Mac System does not recognized the usesScrollButtons.

    I checked ```tabbar->usesScrollButtons();``, and It is return false on Mac.


  • Lifetime Qt Champion

    That's because the macOS style doesn't implement them. As stated in the QTabBar documentation: By default the value is style dependant.. It's not a bug it's a platform guideline. Just check with Safari, open lots of tabs. You won't see any scroll button.



  • @SGaist got it.



  • Thank You @SGaist and @AndyS. I am able to get QToolButton of QTabBar by applying Fusion style.

    I applied Fusion Style By:

    #include "mainwindow.h"
    
    #include <QTimer>
    #include <QtGlobal>
    
    
    #include <QFile>
    
    
    #include <QLocale>
    #include <QSplashScreen>
    #include <QGuiApplication>
    #include <qstylefactory.h>
    
    
    int main(int argc, char *argv[]) {
        QLocale::setDefault(QLocale::system());
    
        QApplication a(argc, argv);
        a.setStyle(QStyleFactory::create("Fusion"));
        
        MainWindow w;
        w.showMaximized();
        return a.exec();
    }