pyqt表格学习

import signal

import requests
# from PyQt5.QtGui.QRawFont import style
from PyQt5.QtGui import QBrush, QColor, QFont, QIcon
from PyQt5.QtWidgets import QApplication, QMessageBox, QMdiSubWindow, QTreeWidgetItem, QTableWidgetItem, QComboBox, \
    QPushButton, QHeaderView, QMenu
from PyQt5 import uic, QtCore, QtWidgets
from PyQt5.QtCore import Qt
from lib.share import SI
from cfg_setting import Win_cfgConnection


#定义主窗口类
class Win_Main:
    def __init__(self):
        #加载主窗口类
        self.ui=uic.loadUi("table.ui")

        # 允许上下文菜单
        self.ui.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.ui.tableWidget.customContextMenuRequested.connect(self.generateMenu)


        # #for循环建立表格
        # # 设置列数
        # self.ui.tableWidget.setColumnCount(4)
        # # 设置行数
        # self.ui.tableWidget.setRowCount(40)
        # for i in range(40):
        #     for j in range(4):
        #         # 行,列,替代符
        #         item = '(%d,%d)'%(i, j)
        #         self.ui.tableWidget.setItem(i, j, QTableWidgetItem(item))






        # #搜索满足条件的cell
        # text='(18,1)'
        #精确查找
        # items=self.ui.tableWidget.findItems(text,QtCore.Qt.MatchExactly)
        #以什么开头
        # items=self.ui.tableWidget.findItems(text,QtCore.Qt.MatchStartWith)

        # # print(items[0].text())
        # if len(items)>0:
        #     item=items[0]
        #     #设置背景色
        #     item.setBackground(QBrush(QColor(0,252,0)))
        #     #设置前景色
        #     item.setForeground(QBrush(QColor(252,0,0)))
        #
        #
        #     row = item.row()
        #
        #     #定位到指定的行
        #     self.ui.tableWidget.verticalScrollBar().setSliderPosition(row)

        # 获取
        # 某一表格内容
        #
        # self.tableWidget.item(1, 1).text()


        # 设置列数
        self.ui.tableWidget.setColumnCount(4)
        # 设置行数
        # self.ui.tableWidget.setRowCount()



        #设置表头
        self.ui.tableWidget.setHorizontalHeaderLabels(['内容1','内容2','内容3','内容4'])
        #设置列平铺
        self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)

        #设置排序
        self.ui.tableWidget.setSortingEnabled(True)
        # 设置列宽
        # self.ui.tableWidget.setColumnWidth(1,6666)
        #设置网格样式
        self.ui.tableWidget.setGridStyle(Qt.DashLine)





        list_equip_temp = ["ip", "addr", "mac", "ver"]
        list_equip_temp2 = ["ip2", "addr2", "mac2", "ver2"]



        row_count = self.ui.tableWidget.rowCount()  # 返回当前行数(尾部)
        self.ui.tableWidget.insertRow(row_count)  # 尾部插入一行
        #插入数据
        for i in range(4):
            self.ui.tableWidget.setItem(row_count, i, QTableWidgetItem(str(list_equip_temp[i])))

        #
        #
        #
        #
        #
        # row_count = self.ui.tableWidget.rowCount()  # 返回当前行数(尾部)
        # self.ui.tableWidget.insertRow(row_count)  # 尾部插入一行
        # for i in range(4):
        #     self.ui.tableWidget.setItem(row_count, i, QTableWidgetItem(str(list_equip_temp2[i])))



        # 插入一行
        row_count = self.ui.tableWidget.rowCount()  # 返回当前行数(尾部)
        self.ui.tableWidget.insertRow(row_count)  # 尾部插入一行

        #加入文本,setItem(行,列,QTableWidgetItem('值'))
        textItem=QTableWidgetItem('小明')
        self.ui.tableWidget.setItem(1,0,textItem)
        #单元格内添加图片
        # textItem = QTableWidgetItem(QIcon('./images/1.jpg'),'图片')
        # self.ui.tableWidget.setItem(0,0,textItem)

        # #单元格显示图片
        # textItem = QTableWidgetItem()
        # textItem.setIcon(QIcon('./image/1.jpg'))
        # #设置单元格内容字体
        # textItem.setFont(QFont('Times',144,QFont.Black))

        #设置行高
        self.ui.tableWidget.setRowHeight(0,100)
        # 合并单元格setspan(行,列,要合并的行数,要合并的列数)
        # self.ui.tableWidget.setSpan(0,0,2,2)


        # #设置居中显示
        # textItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)


        # 插入可选框combox
        # combox = QComboBox()
        # combox.addItem('男')
        # combox.addItem('女')
        # self.ui.tableWidget.setCellWidget(2, 2, combox)


        # pushbutton=QPushButton()
        # pushbutton.setText('删除')
        # self.ui.tableWidget.setCellWidget(2, 3, pushbutton)
        # # self.ui.tableWidget.setCellWidget(2, 0, pushbutton)
        # #表格中的控件触发函数
        # pushbutton.clicked.connect(self.aaa)
        #
        # #将按钮加入表格控件中
        # modifyButton = QPushButton('修改')
        # #修改控件大小
        # modifyButton.setStyleSheet('QPushButton{margin:3px};')
        # self.ui.tableWidget.setCellWidget(2,1,modifyButton)
        # self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)




    def aaa(self):
        print('aaa')
    #pos为坐标
    def generateMenu(self, pos):
        print(pos)

        for i in self.ui.tableWidget.selectionModel().selection().indexes():
            rowNum = i.row()
        # 如果选择的行索引小于2,弹出菜单
        if rowNum < 2:
            menu = QMenu()
            item1 = menu.addAction('菜单项1')
            item2 = menu.addAction('菜单项2')
            item3 = menu.addAction('菜单项3')
            # 将坐标转化为相对于屏幕的
            screenPos = self.ui.tableWidget.mapToGlobal(pos)
            action = menu.exec(screenPos)
            if action == item1:
                print('选择了第一个菜单项', self.ui.tableWidget.item(rowNum, 0).text(),
                      self.ui.tableWidget.item(rowNum, 1).text(),
                      self.ui.tableWidget.item(rowNum, 2).text())
            elif action == item2:
                print('选择了第二个菜单项', self.ui.tableWidget.item(rowNum, 0).text(),
                      self.ui.tableWidget.item(rowNum, 1).text(),
                      self.ui.tableWidget.item(rowNum, 2).text())
            elif action == item3:
                print('选择了第三个菜单项', self.ui.tableWidget.item(rowNum, 0).text(),
                      self.ui.tableWidget.item(rowNum, 1).text(),
                      self.ui.tableWidget.item(rowNum, 2).text())
            else:
                return






#SI中的加载文件内容
SI.loadCfgFile()
app=QApplication([])
# SI.loginWin=Win_Login()
# SI.loginWin.ui.show()

SI.mainWin=Win_Main()
SI.mainWin.ui.show()
app.exec_()




你可能感兴趣的:(1024程序员节)