QToolButton not visible in windows
Asked Answered
C

1

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

Cholinesterase answered 21/7, 2017 at 7:39 Comment(11)
Might want to fix the indentation of the code.Crackleware
I would have expected a call to QWidget::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.Federico
I have tried what you suggested but it didn't work. Can you try to run it and see what is wrong?Cholinesterase
Did you output and check the geometries as I suggested?Federico
@Federico I have edited my answer to include some observationsCholinesterase
@Federico I have tried your suggested experiments. Please check my updated questionCholinesterase
I have started bounty!Cholinesterase
It may be easier to implement your + button as an empty tab, which automatically creates a new tab when it is selected.Susanasusanetta
I tried that but it doesn't look good.Cholinesterase
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,Picasso
@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.Haik
H
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);
Haik answered 31/8, 2017 at 14:53 Comment(4)
It have tried what you have suggested in the past but it didn't work.Cholinesterase
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.Haik
I have removed the stylesheet and tried what you suggested. I will give it a another try and update you.Cholinesterase
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.Haik

© 2022 - 2024 — McMap. All rights reserved.