Summary : This code shows how to change
tab
button of QTabWidget. In order to handle clicked event of
tab button , handle 'clicked' event of that
button.
In this example each
button is a QPushButton with information icon.
Qt Version: 4.7.4
Date Added: 2011-08-28
#include <QtGui>
/**
* Derived Class from QTabWidget.
* Protected QTabWidget::tabBar() method is
* overridden in order to make it accessible.
*/
class MyTabWidget:public QTabWidget
{
public:
MyTabWidget(QWidget* parent =
0):QTabWidget(parent)
{
setParent(parent);
}
//Overridden method from QTabWidget
QTabBar* tabBar()
{
return QTabWidget::tabBar();
}
};
int main(
int argc,
char **argv)
{
QApplication app(argc, argv);
QMainWindow *window = new QMainWindow();
window->setWindowTitle(QString::fromUtf8(
"Set QTabBar Tab Button"));
window->resize(
336,
227);
QWidget *centralWidget = new QWidget(window);
MyTabWidget *tabs = new MyTabWidget(centralWidget);
tabs->setFixedSize(
330,
220);
tabs->addTab(new QWidget(),
"TAB 1");
tabs->addTab(new QWidget(),
"TAB 2");
tabs->addTab(new QWidget(),
"TAB 3");
QPushButton*
button1 = new QPushButton();
button1->setFixedWidth(
20);
button1->setIcon(*(new QIcon(
"information-icon.gif")));
QPushButton*
button2 = new QPushButton();
button2->setFixedWidth(
20);
button2->setIcon(*(new QIcon(
"information-icon.gif")));
QPushButton*
button3 = new QPushButton();
button3->setFixedWidth(
20);
button3->setIcon(*(new QIcon(
"information-icon.gif")));
tabs->tabBar()->setTabButton(
0, QTabBar::LeftSide,((QWidget*)(
button1)));
tabs->tabBar()->setTabButton(
1, QTabBar::LeftSide,((QWidget*)(
button2)));
tabs->tabBar()->setTabButton(
2, QTabBar::LeftSide,((QWidget*)(
button3)));
window->setCentralWidget(centralWidget);
window->show();
return app.exec();
}
Output :
转载请注明原文地址: https://ju.6miu.com/read-1294111.html