赞
踩
这段代码是定义各一个框,框里面放一个列表,列表可被滑动选中,并将选中的表格,进行高亮显示,可以滑动将高亮选中的表格进行取消,表格的状态存放在数据库中。这个展示的视频我上传了,不知道怎么显示不在这里,好像在博客别的地方。
样式是这样的,可以滑动选择和取消。
- //右上框5
- QFrame *borderFrame5 = new QFrame(this);
- borderFrame5->setFrameStyle(QFrame::Box);
- borderFrame5->setGeometry(758,15,292,529);
- borderFrame5->setStyleSheet("QFrame{"
- "border-radius: 5px;"
- "border: 1px solid #455364;"
- "}");
-
-
- QLabel *receiverDataTableTitle = new QLabel("接受以下主机数据:");
-
- receiverDataTableWidget = new NetTableWidget(1);
- receiverDataTableWidget->setFixedSize(290,470);
- //创建的行和列 5行3列
- receiverDataTableWidget->setRowCount(64);
- receiverDataTableWidget->setColumnCount(3);
- //自适应行高
- receiverDataTableWidget->resizeRowsToContents();
-
- //隐藏最左侧的列(第0列)
- QHeaderView *customerVerticalHeader = new QHeaderView(Qt::Vertical);
- customerVerticalHeader->setVisible(false);
-
-
- receiverDataTableWidget->setVerticalHeader(customerVerticalHeader);
- receiverDataTableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- receiverDataTableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- //Qt::ScrollBarAsNeeded 滚动条将根据表格内容是否溢出来显示或者隐藏
- receiverDataTableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
-
- //设置垂直表头的大小策略
- QHeaderView *horizontalHeader = receiverDataTableWidget->horizontalHeader();
- horizontalHeader->setSectionResizeMode(QHeaderView::Fixed); //设置固定大小
-
- QStringList horizontalHeaderLabels;
- horizontalHeaderLabels << "火警" << "故障" << "联动";
- receiverDataTableWidget->setHorizontalHeaderLabels(horizontalHeaderLabels);
- receiverDataTableWidget->setHorizontalHeaderLabels(horizontalHeaderLabels);
-
-
- //一共有64个主机,这里创建一个主机号列表,一个主机有三种状态,所以是111,222,333
- QStringList data;
- for(int i=1;i<195;i++){
- for(int j=0;j<3;j++)
- data.append(QString::number(i));
- }
-
-
- //将主机号显示在界面,同时从数据库读取列表中表格被选中状态
- int row =0;
- int col =0;
- for(const QString &item : data){
- QTableWidgetItem *tableItem = new QTableWidgetItem(item);
- int h=_pDataAccess->cfg_host_event_select(row,col);
- if(h==0)//若没有被选中则界面标记为选中状态
- tableItem->setBackground(QBrush(QColor(25,35,45)));
- else if(h==1)//若已经被选中了,则取消选中状态
- tableItem->setBackground(QBrush(QColor(69,83,100)));
- receiverDataTableWidget->setItem(row,col,tableItem);
- col++;
- if(col==3){
- col=0;
- row++;
- }
- }
-
- //将主机号文本居中
- for (int i = 0; i<64; i++)
- {
- for (int j = 0; j<3; j++)
- {
- receiverDataTableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
- }
- }
-
- receiverDataTableTitle->setStyleSheet("border:none;color:white;");
- horizontalHeader->setStyleSheet("QHeaderView::section{border:0.7px solid #455364;background-color:#19232d;color:white;}");
- // receiverDataTableWidget->setStyleSheet("QTableWidget::item{border:2px solid #455364;background-color:#19232d;color:white;}"
- // "QTableWidget::item:selected{background-color:lightblue;}");
- receiverDataTableWidget->setFocusPolicy(Qt::NoFocus);
- //添加到mainLayout
- QVBoxLayout *upperRightMainLayout = new QVBoxLayout;
- //添加到widget容器中去
- upperRightMainLayout->addWidget(receiverDataTableTitle);
- upperRightMainLayout->addWidget(receiverDataTableWidget);
- upperRightMainLayout->addStretch();
-
- borderFrame5->setLayout(upperRightMainLayout);
这段代码本来使用itemSelectionChanged作为触发信号,但多选时,他会依次叠加发送多次。所以这里的信号是自己重写的onMouseUp。当多选时,鼠标释放时,触发信号。
- //接收主机事件列表
- // 连接表格的单元格修改信号到槽函数itemSelectionChanged
- connect(receiverDataTableWidget, &NetTableWidget::onMouseUp,this->receiverDataTableWidget, [=]() {
- QList<QTableWidgetItem*> selectItems = receiverDataTableWidget->selectedItems();
- qDebug() << "selectedItems("<<selectItems.size()<<")----------";
- for(auto item:selectItems)
- {
- qDebug() << item->row() <<", "<< item->column();
- int h=_pDataAccess->cfg_host_event_select(item->row(),item->column());
- if(h==0)//若没有被选中则界面标记为选中状态
- item->setBackground(QBrush(QColor(69,83,100)));
- else if(h==1)//若已经被选中了,则取消选中状态
- item->setBackground(QBrush(QColor(25,35,45)));
- _pDataAccess->cfg_host_event(item->row(),item->column());
- }
- receiverDataTableWidget->clearSelection();
- });
重新的方法为:
- class NetTableWidget : public QTableWidget
- {
- Q_OBJECT
-
- public:
- NetTableWidget(int ll){
-
- };
- void mouseReleaseEvent(QMouseEvent *event){
- emit onMouseUp();
- qDebug()<<"mouse up event";
- QTableWidget::mouseReleaseEvent(event); // 将事件传递给父类处理
- }
-
- signals:
- void onMouseUp();
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。