Close button only for some tabs in Qt

后端 未结 7 1951
天命终不由人
天命终不由人 2021-02-05 05:22

I am using Qt for an assignment I have for college, and I want to use QTabWidget to display a chat window much like Pidgin\'s. I want to make the \"group chat\" tab

相关标签:
7条回答
  • 2021-02-05 05:53

    Find the bar (it is private, so use findChild()) and remove the buttons. Documentation claims that close buttons may be placed on left side too.

    QTabBar *tabBar = ui->tabWidget->findChild<QTabBar *>();
    tabBar->setTabButton(0, QTabBar::RightSide, 0);
    tabBar->setTabButton(0, QTabBar::LeftSide, 0);
    
    0 讨论(0)
  • 2021-02-05 05:54

    The best way for adding a pushbutton to some tabs and not in other is to define a subclass of QTabWidget for taking the QTabBar that is a potected! The code below is tested and it works:

    //TabWidget.h
    
    #ifndef TABWIDGET_H
    #define TABWIDGET_H
    #include <QTabWidget>
    
    class TabWidget : public QTabWidget {
    public:
        TabWidget(QWidget *parent);
        ~TabWidget();
        QTabBar *tabBar() const;
    };
    #endif  /* TABWIDGET_H */
    
    //TabWidget.cpp
    
    #include "TabWidget.h"
    TabWidget::TabWidget(QWidget * p=0) : QTabWidget(p) {}
    
    TabWidget::~TabWidget() {}
    
    QTabBar * TabWidget::tabBar() const {return QTabWidget::tabBar();}
    

    For using this subclass and create a new tab with a custom button you have to following this instructions ( ui->tabWidget is a QTabWidget with setClosableTabs=false):

    TabWidget *t = (TabWidget *) ui->tabWidget;
    t->addTab(new QWidget, "empty");
    QTabBar *tab = t->tabBar();
    QPushButton *b = new QPushButton();
    b->setText("x");
    tab->setTabButton(tab->count() -1, QTabBar::RightSide, b);
    connect(b,SIGNAL(...),this,SLOT(...));
    
    0 讨论(0)
  • 2021-02-05 05:55

    Although tabBar() is indeed protected, Klaus pointed into the right direction. Simply subclass QTabWidget and implement a wrapper method.

    0 讨论(0)
  • 2021-02-05 05:57

    Hallo,

    I guess this post won't help the author of this thread but perhaps someone else wanders over here.

    In most cases a non-closable tab should not only ignore the closevent it also should not show a close symbol in its corner. A nice way to reach this is to modify the QTabBar which is inside the QTabWidget.

    Example:

    // let tabWidget be a QTabWidget with at least one page
    
    QPushButton *closeButton = new QPushButton();
    // set icon and size for PushButton, ... 
    // connect Signal clicked() from closeButton with Slot closeCurrentTab(), ...
    
    // next line sets closeButton in right corner on tab with index 0
    tabWidget->tabBar()->setTabButton(0, QTabBar::RightSide, closeButton);
    
    0 讨论(0)
  • 2021-02-05 05:58

    You should reimplement your widget's event(Event *e) method, check the type of e, find out CloseEvents, and call parent class's event when you can allow tab to close, or e->ignore() when you do not want it.

    Note, then you must parent's event() handle othr events, so do not accept(), reject() or forget them Ж)

    0 讨论(0)
  • 2021-02-05 06:06

    I guess you can handle the tabCloseRequest signal and decide whether u'll close a given tab or not

    http://doc.qt.io/archives/4.6/qtabwidget.html#tabCloseRequested

    Edit: I created a small example to check it out. My example is a simple QtGui application with a mainwindow that has a tabwidget. I then added the tabCloseRequested slot. Here is the code

    void MainWindow::on_tabWidget_tabCloseRequested(int index)
    {
       if(someCondition){
           return;
       } else if(anotherCondition){
           ui->tabWidget->removeTab(index);
       }
    }
    

    From this example only tabs where the condition doesn't apply will be closed.

    0 讨论(0)
提交回复
热议问题