Use of QTabWidget

Common property settings of QTabWidget.

  1. Set the scroll bar to hide
table->horizontalScrollBar()->setVisible(false);
table->verticalScrollBar()->setVisible(false);

  1. Set the entire line selected
table->setSelectionBehavior(QAbstractItemView::SelectRows);

  1. Setting is not editable
table->setEditTriggers(QAbstractItemView::NoEditTriggers); 

  1. Set the number of columns
table->setColumnCount(3);

  1. Set hidden wireframe
table->setShowGrid(false);

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

  1. Set the header to hide
table->horizontalHeader()->setHidden(true);
table->verticalHeader()->setHidden(true);

  1. Set the header name
QStringList header; header<<"子系统路口"<<"上移";
table->setHorizontalHeaderLabels(header);

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

  1. Add right-click pop-up function to the table
table->setContextMenuPolicy(Qt::CustomContextMenu);
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("");
label->setPixmap(QPixmap(":/image/grid").scaled(30,30));
ui->matrixViewTable->setCellWidget(0,i,label);

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

class QPixmapItemdele : public QStyledItemDelegate
{
    
    
public:
    QPixmapItemdele(QObject* parent = 0):QStyledItemDelegate(parent){
    
     }
    //在委托类的paint中画图
    virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    
    
        if(index.data(Qt::DisplayRole).canConvert<QPixmap>())
        {
    
    
            QPixmap pix = index.data(Qt::DisplayRole).value<QPixmap>();
            painter->drawPixmap(option.rect,pix);
        }
        QStyledItemDelegate::paint(painter,option,index);
    }
};

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();
        ui->matrixViewTable->setItem(0,i,item);
        item->setData(Qt::DisplayRole,QVariant::fromValue<QPixmap>(QPixmap(":/image/grid").scaled(30,30)));
    }

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:

ui->matrixViewTable->setIconSize(QSize(30,30));

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);
ui->tableView->resizeRowsToContents();//所有行高度自适应
ui->tableView->resizeColumnsToContents();//所有行宽度自适应

Guess you like

Origin blog.csdn.net/locahuang/article/details/110198005