Use of QTabWidget

Common property settings of QTabWidget.

  1. Set the scroll bar to hide

  1. Set the entire line selected

  1. Setting is not editable

  1. Set the number of columns

  1. Set hidden wireframe

  1. Set column width
table->setColumnWidth(0, 20);
table->setColumnWidth(1, 128);
table->setColumnWidth(2, 20);

  1. Set the header to hide

  1. Set the header name
QStringList header; header<<"子系统路口"<<"上移";

  1. Set header width content adaptive
table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);

  1. Add right-click pop-up function to the table
connect(table, SIGNAL(customContextMenuRequested ( const QPoint&)), this , SLOT(rightClickedOperationPRJ(const QPoint&))); //右键

  1. Displaying pictures
    in tables There are several ways to display pictures in tables in QTableWidget: You
    need to load pictures as resources first.

1) Method 1: Create directly when creating QTableWidgetItem, such as

tableWdiget->setItem(0,1,new QTableWidgetItem(QIcon(":/image/cpu")," "));

2) Method 2: Create a label in the item, and display the picture through the label's setPixMap function:

QLabel *label = new QLabel("");

3) Method 3: By inheriting the QStyledItemDelegate class and using delegates to display pictures in the item:

class QPixmapItemdele : public QStyledItemDelegate
    QPixmapItemdele(QObject* parent = 0):QStyledItemDelegate(parent){
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
            QPixmap pix =<QPixmap>();

Then set a delegate for the tablewidget, and then you can set a specific picture of an item:

 	ui->matrixViewTable->setItemDelegate(new QPixmapItemdele());
    for(int i=0; i<ui->matrixViewTable->columnCount(); i++)
        QTableWidgetItem *item = new QTableWidgetItem();

The above three methods:

  • Method 1: Create an Icon directly on the item, and each Item in the table is represented as the icon on the left, and the text corresponding to the icon on the right. Even if the text is empty, there will be a lot of space left, resulting in a small icon on the left.

    You can use the following statement to set the size of the icon on the left:


But there is still space left by text on the right. If you want the icon to fill the entire item, this method cannot be achieved.

  • The second method is to create a label in the item to setPixMap to make the icon fill the entire item, but the displayed icon will not be selected with a mouse click, or may be able to handle the click event (not tried, I wonder if it works).

  • Method 3 inheriting the delegation can make the picture fill the entire item, and when the picture is selected at the same time, a selection box will appear, so that there is no response when clicking it like the method 2.

  1. Set the cell text to be centered, and Zhang San’s data is set to be displayed in the center
table->item(0, 0)->setTextAlignment(Qt::AlignCenter);
table->item(0, 1)->setTextAlignment(Qt::AlignCenter);

  1. Set the cell text color, Zhang San’s data is set to red
table->item(0, 0)->setForeground(QBrush(QColor(255, 0, 0))); 
 table->item(0, 1)->setForeground(QBrush(QColor(255, 0, 0))); 

  1. Bold font
table->item(0, 0)->setFont( QFont( "Times", 10, QFont::Black ) );
table->item(0, 1)->setFont( QFont( "Times", 10, QFont::Black ) );

  1. Set the sorting method, display in descending order of age
 table->sort(3, Qt::DescendingOrder);

Guess you like