10

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

jpo38
  • 20,821
  • 10
  • 70
  • 151
abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • Might want to fix the indentation of the code. – tambre Jul 21 '17 at 07:45
  • I would have expected a call to [`QWidget::updateGeometry`](http://doc.qt.io/qt-5/qwidget.html#updateGeometry) at the end of `movePlusButton` as the `sizeHint` has been affected. Apart from that I cant see any obvious issue. Add code at the end of `movePlusButton` to print out the `rect()` of the `QTabBar` and the `geometry()` of `new_button_` to see if the values all make sense. – G.M. Jul 21 '17 at 09:17
  • I have tried what you suggested but it didn't work. Can you try to run it and see what is wrong? – abhiarora Jul 22 '17 at 19:01
  • Did you output and check the geometries as I suggested? – G.M. Jul 24 '17 at 08:46
  • @G.M. I have edited my answer to include some observations – abhiarora Jul 25 '17 at 19:01
  • @G.M. I have tried your suggested experiments. Please check my updated question – abhiarora Jul 25 '17 at 19:01
  • I have started bounty! – abhiarora Aug 27 '17 at 15:54
  • It may be easier to implement your + button as an empty tab, which automatically creates a new tab when it is selected. – m7913d Aug 28 '17 at 08:50
  • I tried that but it doesn't look good. – abhiarora Aug 28 '17 at 10:59
  • It seems to me that there is some issue with parent child replationship..., I suggest you to create layout inside tab bar class and add your button in that layout. It will be more helpful if you can submit sample working code so that someone can directly copy paste and trace the actual issue. Thanks, – Rajesh Rathod Aug 31 '17 at 04:47
  • 1
    @abhiarora: Are you using any stylesheet for your application? Because I compiled your code and displayed your TabBar in a standard QMainWindow and the "+" button is displayed correctly and behaves as expected. – jpo38 Aug 31 '17 at 14:47

1 Answers1

3

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);
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • It have tried what you have suggested in the past but it didn't work. – abhiarora Sep 02 '17 at 20:15
  • What did you try? Have you disabled your stylesheet? For me, your question is wrongly asked. You should have asked "how to make my button visible considering the stylesheet I use?" and post the stylesheet...because I bet that's the root cause of your problem. – jpo38 Sep 03 '17 at 18:11
  • I have removed the stylesheet and tried what you suggested. I will give it a another try and update you. – abhiarora Sep 03 '17 at 18:14
  • The first think is to check if the button looks ok after you removed the stylesheet. And it should, as I see it displayed correctly when using your code under Windows. – jpo38 Sep 03 '17 at 20:52