c++windowsqtqtabbarqtoolbutton

QToolButton not visible in windows


I have derived the class of QTabBar to implement "+" (new tab button) button using QToolButton (similar to google chrome). However, it is working in my Linux machine but doesn't work in my windows machine. By not working I mean QToolButton is not visible in my windows machine but it is visible in my Linux machine (Ubuntu). I am not able to debug it further as I have tried few experiments to understand the reason but it didn't work.

My Source file:

#include "tabbar.h"

TabBar::TabBar(QWidget *parent) : QTabBar(parent)
{
    new_button_ = new QToolButton(this);
    new_button_->setObjectName(QStringLiteral("AddButton"));
    new_button_->setText("+");
    new_button_->setFixedSize(QSize(20, 20));
    connect(new_button_, SIGNAL(released()), this, SLOT(emit_new()));
    movePlusButton();
}

QSize TabBar::sizeHint(void) const
{
    QSize old = QTabBar::sizeHint();
    return QSize(old.width() + 45, old.height());
}

void TabBar::emit_new(void)
{
    emit newClicked();
}

void TabBar::movePlusButton(void)
{
    quint64 totalWidth = 0;
    for (long i=0; i < count(); i++)
        totalWidth += tabRect(i).width();

    quint64 h = geometry().top();
    quint64 tab_height = height();
    quint64 w = width();

    if (totalWidth > w)
        new_button_->move(w-40, tab_height - 30);
    else
        new_button_->move(totalWidth + 5, tab_height - 30);
}

void TabBar::resizeEvent(QResizeEvent *p_evt)
{
    QTabBar::resizeEvent(p_evt);
    movePlusButton();
}

void TabBar::tabLayoutChange(void)
{
    QTabBar::tabLayoutChange();
    movePlusButton();
}

My Header File:

#ifndef TABBAR_H
#define TABBAR_H

#include <QObject>
#include <QToolButton>
#include <QTabBar>
#include <QResizeEvent>
#include <QLabel>

class TabBar : public QTabBar {
Q_OBJECT

public:
    TabBar(QWidget *parent=nullptr);
    ~TabBar() { }

    void movePlusButton(void);


    void resizeEvent(QResizeEvent *p_evt) override;
    void tabLayoutChange(void) override;
    QSize sizeHint(void) const override;

private slots:
    void emit_new(void);

signals:
    void newClicked(void);

private:
    QToolButton *new_button_;
};

#endif // TABBAR_H

EDIT:

I have tried few more experiments and got to know QToolButton is hiding behind region next to Tab bars. Please refer the screenshot.

enter image description here


Solution

  • Apparently, your application uses a stylesheet or something to customize the display and this is incompatible with your TabBar class.

    Downloaded your code, wrote a simple main:

    #include <QApplication>
    #include <QMainWindow>
    #include "tabbar.h"
    
    int main( int argc, char* argv[] )
    {
        QApplication app(argc, argv);
    
        QMainWindow wnd;
    
        TabBar* tabBar = new TabBar(&wnd);
        wnd.setCentralWidget( tabBar );
    
        tabBar->addTab( "Foo" );
    
        wnd.show();
    
        return app.exec();
    }
    

    compiled and executed on Windows and got that (tested classic and aero style): enter image description here

    So apparently your code is fine. However, if you customized the QTabBar rendering through a stylesheet (what I suspect when I see how it looks in your GUI), you may need to adapt yourcode (probably movePlusButton as it has some values hardcoded that may be incompatible with the current display style):

    if (totalWidth > w)
        new_button_->move(w-40, tab_height - 30);
    else
        new_button_->move(totalWidth + 5, tab_height - 30);