首页 > 代码库 > Qt-QTableWidget自由均分和能点击修改和去掉虚框

Qt-QTableWidget自由均分和能点击修改和去掉虚框

直接上代码

 
 1 ui.outFlag_tableWidget->setRowCount (2); 2     ui.outFlag_tableWidget->setColumnCount (3); 3     QStringList mHeader; 4     QStringList mVertical; 5     mHeader << tr ("未来24小时") << tr ("未来48小时") << tr ("未来72小时"); 6     mVertical << "太阳F10.7指数" << "地磁指数AP"; 7     ui.outFlag_tableWidget->setHorizontalHeaderLabels (mHeader); 8     ui.outFlag_tableWidget->setVerticalHeaderLabels (mVertical); 9     ui.outFlag_tableWidget->setSelectionMode (QAbstractItemView::SingleSelection);10     ui.outFlag_tableWidget->horizontalHeader()->setResizeMode (QHeaderView::Stretch);11     ui.outFlag_tableWidget->verticalHeader()->setResizeMode (QHeaderView::Stretch);12     ui.outFlag_tableWidget->horizontalHeader()->setClickable (false);13     ui.outFlag_tableWidget->setItemDelegate (new NoFocusDelegate());14     // lyy : 2016/8/29 14:33:56 说明:outFlag_tableWidget15     16     for (int i = 0; i < ui.outFlag_tableWidget->columnCount(); i++)17     {18         QTableWidgetItem *columnHeaderItem0 = ui.outFlag_tableWidget->horizontalHeaderItem (i);19         columnHeaderItem0->setBackground (QBrush (QColor (255, 0, 0)));20         columnHeaderItem0->setTextColor (QColor (255, 255, 255));21         /* QTableWidgetItem *newItem = new QTableWidgetItem (tr ("%1").arg (22                      (i + 1) * (1 + 1)));23          ui.outFlag_tableWidget->setItem (0, i, newItem);24          int a = ui.outFlag_tableWidget->itemAt (0, 1)->text().toInt();*/25         // QMessageBox::information (this, "test", QString::number (a));26     }27     28     for (int i = 0; i < ui.outFlag_tableWidget->rowCount(); i++)29     {30         QTableWidgetItem *columnHeaderItem0 = ui.outFlag_tableWidget->verticalHeaderItem (i);31         columnHeaderItem0->setBackground (QBrush (QColor (255, 0, 0)));32         columnHeaderItem0->setTextColor (QColor (255, 255, 255));33     }34     35     ui.outFlag_tableWidget->horizontalHeader()->setStyleSheet ("QHeaderView::section{border : 0;gridline-color::rgb(0,0,0) ;background-color:rgb(0,0,0)}");36     ui.outFlag_tableWidget->verticalHeader()->setStyleSheet ("QHeaderView::section{border : 0;gridline-color::rgb(0,0,0) ; background-color:rgb(0,0,0)}");

添加数据

 1 void DailyPaper::WritePrecateResult() 2 { 3     if (this->mResultString.isEmpty()) 4     { 5         QMessageBox::information (this, "test", "请点击预报按钮!"); 6     } 7      8     else 9         if (!ui.SpaceEnvermentReview_textEdit->toPlainText().isEmpty())10         {11             QMessageBox::information (this, "test", "确定要重新填写吗!");12         }13         14         else15         {16             QStringList listTemp = this->mResultString.simplified().split (QRegExp ("[#|*]"), QString::SkipEmptyParts); //把每一个块装进一个QStringList中17             ui.SpaceEnvermentReview_textEdit->setPlainText (listTemp[0]);18             ui.outThreeDay_textEdit->setPlainText (listTemp[1]);19         }20         21     for (int j = 0; j < ui.outFlag_tableWidget->rowCount(); j++)22     {23         for (int i = 0; i < ui.outFlag_tableWidget->columnCount(); i++)24         {25             QTableWidgetItem *newItem = new QTableWidgetItem (tr ("%1").arg (26                         (i + 1) * (1 + 1)));27             ui.outFlag_tableWidget->setItem (j, i, newItem);28             //int a = ui.outFlag_tableWidget->itemAt (0, 1)->text().toInt();29             // QMessageBox::information (this, "test", QString::number (a));30         }31     }32     33     for (int j = 0; j < ui.outEventStart_tableWidget->rowCount(); j++)34     {35         for (int i = 0; i < ui.outEventStart_tableWidget->columnCount(); i++)36         {37             QTableWidgetItem *newItem = new QTableWidgetItem (tr ("%1").arg (38                         (i + 1) * (1 + 1)));39             ui.outEventStart_tableWidget->setItem (j, i, newItem);40         }41     }42 }

 去虚框

 1 #include "NoFocusDelegate.h" 2  3  4 NoFocusDelegate::NoFocusDelegate (void) 5 { 6 } 7  8 void NoFocusDelegate::paint (QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 9 {10     QStyleOptionViewItem itemOptin (option);11     12     if (itemOptin.state & QStyle::State_HasFocus)13     {14         itemOptin.state = itemOptin.state ^ QStyle::State_HasFocus;15     }16     17     //    QStyledItemDelegate::paint(painter,itemOptin,index);18     QStyledItemDelegate::paint (painter, itemOptin, index);19 }20 NoFocusDelegate::~NoFocusDelegate (void)21 {22 }
 1 #pragma once 2 #include <QPainter> 3 #include <QStyleOptionViewItem> 4 #include <QStyledItemDelegate> 5 #include <QItemDelegate> 6 class NoFocusDelegate: public QStyledItemDelegate 7 { 8     public: 9         NoFocusDelegate (void);10         void paint (QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;11         ~NoFocusDelegate (void);12 };

 

Qt-QTableWidget自由均分和能点击修改和去掉虚框