QTableWidget/QTableView使用总结

常用属性

//设置表头
QStringList header;
header<<tr("name")<<tr("last modify time")<<tr("type")<<tr("size");
tableWidget->setHorizontalHeaderLabels(header);

// 禁止表格编辑
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

// 隐藏表头
tableWidget->horizontalHeader()->setVisible(false);
tableWidget->verticalHeader()->setVisible(false);

// 设置表格选择模式
tableWidget->setSelectionBehavior();
QAbstractItemView.SelectItems
QAbstractItemView.SelectRows
QAbstractItemView.SelectColumns

tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
QAbstractItemView.NoSelection
QAbstractItemView.SingleSelection
QAbstractItemView.MultiSelection

// 在单元格中加入控件
QComboBox *comBox = new QComboBox(); 
comBox->addItem("Y"); 
comBox->addItem("N"); 
tableWidget->setCellWidget(0,2,comBox); 
// 获取单元格中的控件
QWidget * widget=ui->tableWidget->cellWidget(i,0);//获得widget   
QComboBox *combox=(QComboBox*)widget;//强制转化为QComboBox     
QString string=combox->currentText();  

// 设置隔行换色
tableWidget->setAlternatingRowColors(true);
/* 样式表不设在section中 */
QTableWidget 
{
	alternate-background-color: blue;
}

// 获得单击单元格的内容
connect(table, &QTableWidget::itemDoubleClicked, this, [=](QTableWidgetItem *item)
{
    qDebug() << item->text() << endl;
});

// 右键菜单
tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(table, &QTableWidget::customContextMenuRequested, this, [=]()
{
	auto menu = new QMenu;
	auto ac_open = new QAction("打开");
	menu->addAction(ac_open);

	menu->exec(QCursor::pos());
});

// 右键单元格菜单
table->viewport()->installEventFilter(this);
bool QtTest2::eventFilter(QObject* watched, QEvent* e)
{
	if (watched == table->viewport() && e->type() == QEvent::ContextMenu)
	{
		auto item = table->currentItem();
        if (item)
	    {
			qDebug() << item->row() << endl;
			auto menu = new QMenu;
			auto ac_open = new QAction("打开");
			menu->addAction(ac_open);

			menu->exec(QCursor::pos());
		}
		else
		{
			qDebug() << "No valued cell selected!" << endl;
		}
	}

	return QWidget::eventFilter(watched, e);
}

 

样式表

QTableView
{     
    color: white;
    gridline-color: white;                           /*表格内框颜色*/     
    background-color: rgb(27, 27, 27);               /*表格内背景色*/     
    alternate-background-color: rgb(64, 64, 64);     
    selection-color: white;                          /*选中区域的文字颜色*/     
    selection-background-color: rgb(77, 77, 77);     /*选中区域的背景色*/     
    border: 1px white;                               /*边框像素和颜色*/   
    border-radius: 0px;                              /*边框圆角半径*/   
    padding: 1px 1px; 
}  

/*左上角的button样式*/ 
QTableView QTableCornerButton::section
{   
   color: white;     
   background-color: skyblue;
   border: 1px solid white;     
   border-radius:0px;     
   border-color: rgb(64, 64, 64);  
}

/*表头样式*/ 
QHeaderView::section
{    
    color: white;       
    font: 16pt "Cantarell";
    background-color: skyblue;       
    border:0px solid rgb(191,191,191);     
    border-left-color: rgba(255, 255, 255, 0);     
    border-top-color: rgba(255, 255, 255, 0);     
    border-radius:0px;     
    /*min-height:30px;*/ 
}

 

给左上角空白部分设置文字 

void MainWindow::init()
{
    QAbstractButton* btn = findChild<QAbstractButton*>();
    if (btn)
    {
        btn->setText("ID");
        btn->installEventFilter(this);
        QStyleOptionHeader opt;
        opt.text = btn->text();
        QSize s = (btn->style()->sizeFromContents(QStyle::CT_HeaderSection, &opt, QSize(), btn).
                   expandedTo(QApplication::globalStrut()));
        if (s.isValid())
            ui->tableWidget->verticalHeader()->setMinimumWidth(s.width());
    }
}
 
bool MainWindow::eventFilter(QObject *o, QEvent *e)
{
    if (e->type() == QEvent::Paint)
    {
        QAbstractButton* btn = qobject_cast<QAbstractButton*>(o);
        if (btn)
        {
            // paint by hand (borrowed from QTableCornerButton)
            QStyleOptionHeader opt;
            opt.init(btn);
            QStyle::State state = QStyle::State_None;
            if (btn->isEnabled())
                state |= QStyle::State_Enabled;
            if (btn->isActiveWindow())
                state |= QStyle::State_Active;
            if (btn->isDown())
                state |= QStyle::State_Sunken;
            opt.state = state;
            opt.rect = btn->rect();
            opt.text = btn->text(); 
            opt.position = QStyleOptionHeader::OnlyOneSection;
            QStylePainter painter(btn);
            painter.drawControl(QStyle::CE_Header, opt);
            return true;
        }
    }
    return false;
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页