当前位置:   article > 正文

QTableWidget实现列表界面,滑动选择,并存储数据库_qt widget界面库 滑动

qt widget界面库 滑动

 这段代码是定义各一个框,框里面放一个列表,列表可被滑动选中,并将选中的表格,进行高亮显示,可以滑动将高亮选中的表格进行取消,表格的状态存放在数据库中。这个展示的视频我上传了,不知道怎么显示不在这里,好像在博客别的地方。

样式是这样的,可以滑动选择和取消。

  1. //右上框5
  2. QFrame *borderFrame5 = new QFrame(this);
  3. borderFrame5->setFrameStyle(QFrame::Box);
  4. borderFrame5->setGeometry(758,15,292,529);
  5. borderFrame5->setStyleSheet("QFrame{"
  6. "border-radius: 5px;"
  7. "border: 1px solid #455364;"
  8. "}");
  9. QLabel *receiverDataTableTitle = new QLabel("接受以下主机数据:");
  10. receiverDataTableWidget = new NetTableWidget(1);
  11. receiverDataTableWidget->setFixedSize(290,470);
  12. //创建的行和列 53
  13. receiverDataTableWidget->setRowCount(64);
  14. receiverDataTableWidget->setColumnCount(3);
  15. //自适应行高
  16. receiverDataTableWidget->resizeRowsToContents();
  17. //隐藏最左侧的列(第0列)
  18. QHeaderView *customerVerticalHeader = new QHeaderView(Qt::Vertical);
  19. customerVerticalHeader->setVisible(false);
  20. receiverDataTableWidget->setVerticalHeader(customerVerticalHeader);
  21. receiverDataTableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  22. receiverDataTableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  23. //Qt::ScrollBarAsNeeded 滚动条将根据表格内容是否溢出来显示或者隐藏
  24. receiverDataTableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  25. //设置垂直表头的大小策略
  26. QHeaderView *horizontalHeader = receiverDataTableWidget->horizontalHeader();
  27. horizontalHeader->setSectionResizeMode(QHeaderView::Fixed); //设置固定大小
  28. QStringList horizontalHeaderLabels;
  29. horizontalHeaderLabels << "火警" << "故障" << "联动";
  30. receiverDataTableWidget->setHorizontalHeaderLabels(horizontalHeaderLabels);
  31. receiverDataTableWidget->setHorizontalHeaderLabels(horizontalHeaderLabels);
  32. //一共有64个主机,这里创建一个主机号列表,一个主机有三种状态,所以是111,222333
  33. QStringList data;
  34. for(int i=1;i<195;i++){
  35. for(int j=0;j<3;j++)
  36. data.append(QString::number(i));
  37. }
  38. //将主机号显示在界面,同时从数据库读取列表中表格被选中状态
  39. int row =0;
  40. int col =0;
  41. for(const QString &item : data){
  42. QTableWidgetItem *tableItem = new QTableWidgetItem(item);
  43. int h=_pDataAccess->cfg_host_event_select(row,col);
  44. if(h==0)//若没有被选中则界面标记为选中状态
  45. tableItem->setBackground(QBrush(QColor(25,35,45)));
  46. else if(h==1)//若已经被选中了,则取消选中状态
  47. tableItem->setBackground(QBrush(QColor(69,83,100)));
  48. receiverDataTableWidget->setItem(row,col,tableItem);
  49. col++;
  50. if(col==3){
  51. col=0;
  52. row++;
  53. }
  54. }
  55. //将主机号文本居中
  56. for (int i = 0; i<64; i++)
  57. {
  58. for (int j = 0; j<3; j++)
  59. {
  60. receiverDataTableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
  61. }
  62. }
  63. receiverDataTableTitle->setStyleSheet("border:none;color:white;");
  64. horizontalHeader->setStyleSheet("QHeaderView::section{border:0.7px solid #455364;background-color:#19232d;color:white;}");
  65. // receiverDataTableWidget->setStyleSheet("QTableWidget::item{border:2px solid #455364;background-color:#19232d;color:white;}"
  66. // "QTableWidget::item:selected{background-color:lightblue;}");
  67. receiverDataTableWidget->setFocusPolicy(Qt::NoFocus);
  68. //添加到mainLayout
  69. QVBoxLayout *upperRightMainLayout = new QVBoxLayout;
  70. //添加到widget容器中去
  71. upperRightMainLayout->addWidget(receiverDataTableTitle);
  72. upperRightMainLayout->addWidget(receiverDataTableWidget);
  73. upperRightMainLayout->addStretch();
  74. borderFrame5->setLayout(upperRightMainLayout);

 这段代码本来使用itemSelectionChanged作为触发信号,但多选时,他会依次叠加发送多次。所以这里的信号是自己重写的onMouseUp。当多选时,鼠标释放时,触发信号。

  1. //接收主机事件列表
  2. // 连接表格的单元格修改信号到槽函数itemSelectionChanged
  3. connect(receiverDataTableWidget, &NetTableWidget::onMouseUp,this->receiverDataTableWidget, [=]() {
  4. QList<QTableWidgetItem*> selectItems = receiverDataTableWidget->selectedItems();
  5. qDebug() << "selectedItems("<<selectItems.size()<<")----------";
  6. for(auto item:selectItems)
  7. {
  8. qDebug() << item->row() <<", "<< item->column();
  9. int h=_pDataAccess->cfg_host_event_select(item->row(),item->column());
  10. if(h==0)//若没有被选中则界面标记为选中状态
  11. item->setBackground(QBrush(QColor(69,83,100)));
  12. else if(h==1)//若已经被选中了,则取消选中状态
  13. item->setBackground(QBrush(QColor(25,35,45)));
  14. _pDataAccess->cfg_host_event(item->row(),item->column());
  15. }
  16. receiverDataTableWidget->clearSelection();
  17. });

 重新的方法为:

  1. class NetTableWidget : public QTableWidget
  2. {
  3. Q_OBJECT
  4. public:
  5. NetTableWidget(int ll){
  6. };
  7. void mouseReleaseEvent(QMouseEvent *event){
  8. emit onMouseUp();
  9. qDebug()<<"mouse up event";
  10. QTableWidget::mouseReleaseEvent(event); // 将事件传递给父类处理
  11. }
  12. signals:
  13. void onMouseUp();
  14. };

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/81566
推荐阅读
相关标签
  

闽ICP备14008679号