窗口切换之较为高级的demo(未完待续)

$ cat qtable2_tree3_frame1.py 
#!/usr/bin/env python
# coding=utf-8
from PyQt5.QtCore import QRegExp, Qt, QPoint  # noqa: I202
from PyQt5.QtGui import QIcon, QRegExpValidator,QIcon, QBrush, QColor,QCursor 
from PyQt5.QtWidgets import (
    QApplication,
    QTreeWidget,
    QTreeWidgetItem, 
    QMainWindow,
    QStackedWidget,
    QMenu,
    QAction,
    QTextEdit,
    QCheckBox,
    QColorDialog,
    QComboBox,
    QDialog,
    QFormLayout,
    QGridLayout,
    QGroupBox,
    QHBoxLayout,
    QHeaderView,
    QLabel,
    QLineEdit,
    QMessageBox,
    QPushButton,
    QSpinBox,
    QTableWidget,
    QTableWidgetItem,
    QTabWidget,
    QVBoxLayout,
    QWidget,
)
class myTree(QTreeWidget):
    def contextMenuEvent(self, *xxx):
        try:
            for i in xxx:
                print("xxx i: %s"%i)
        except:
            pass
        item=self.currentItem()
        print('right clicked Key=%s,value=%s'%(item.text(0),item.text(1)))
        print("!!!cur next: %s"%item.child(1))
        print("!!!cur childCount: %s"%item.childCount())
        try:
            print("!!!!cur next: %s"%item.child(0))
            print("cur next: %s"%item.child(0).text(0))
        except:
            print("cur next: %s"%item.child(0))
        try:
            print("above : %s"%self.itemAbove(item).text(0)) #is not None
            print("currentItem parent is: %s"%item.parent().text(0))
        except:
            print("above : None")

        mkdirMenu = QMenu(self)
        mkdirAction = QAction('create dir', self)
        mkdirMenu.addAction(mkdirAction)
        mkdirAction.triggered.connect(self.mkdirFun)
        mkdirMenu.popup(self.mapToGlobal(i.pos()))
    def mkdirFun(self):
        print("mkdirFun is exec")

class OpenDialog(QDialog, QWidget):
    """This dialog is shown to user to select which remote database to load."""

    def __init__(self, plugin=0):
        super(OpenDialog, self).__init__()
        self._plugin = plugin
        self._projects = None
        self._databases = None

        # General setup of the dialog
        self.setWindowTitle("Open from Remote Server")
        #icon_path = self._plugin.plugin_resource("download.png")
        #self.setWindowIcon(QIcon(icon_path))
        self.resize(900, 450)

        # Setup of the layout and widgets
        layout = QVBoxLayout(self)
        main = QWidget(self)
        main_layout = QGridLayout(main)
        layout.addWidget(main)

        self._left_side = QWidget(main)
        self._left_layout = QVBoxLayout(self._left_side)
        self._projects_table = QTableWidget(0, 1, self._left_side)
        self._projects_table.setHorizontalHeaderLabels(("Projects",))
        self._projects_table.horizontalHeader().setSectionsClickable(False)
        self._projects_table.horizontalHeader().setStretchLastSection(True)
        self._projects_table.verticalHeader().setVisible(False)
        self._projects_table.setSelectionBehavior(QTableWidget.SelectRows)
        self._projects_table.setSelectionMode(QTableWidget.SingleSelection)
        ##self._projects_table.itemSelectionChanged.connect(
            ##self._project_clicked
        ##)
        self._left_layout.addWidget(self._projects_table)
        ###zj_add
        self._tree = myTree()#QTreeWidget() # (self._left_side)
        self._tree.setColumnCount(2)
        self._tree.setHeaderLabels(['Key','Value'])
        root = QTreeWidgetItem(self._tree)
        root.setText(0, "Root")
        root.setIcon(0, QIcon('./image/wifi.png'))
        brush_red = QBrush(Qt.red)
        root.setBackground(0, brush_red)
        brush_blue = QBrush(Qt.blue)
        root.setBackground(1, brush_blue)
        self._left_layout.addWidget(self._tree)
        self._tree.setColumnWidth(0,150)

        #设置子节点1
        child1=QTreeWidgetItem()
        child1.setText(0,'child1')
        child1.setText(1,'ios')
        child1.setIcon(0,QIcon('./images/wifi.png'))

        #todo 优化1 设置节点的状态
        child1.setCheckState(0,Qt.Checked)

        root.addChild(child1)
        #设置子节点2
        child2=QTreeWidgetItem(root)
        child2.setText(0,'child2')
        child2.setText(1,'')
        child2.setIcon(0,QIcon('./images/wifi.png'))

        #设置子节点3
        child3=QTreeWidgetItem(child2)
        child3.setText(0,'child3')
        child3.setText(1,'android')
        child3.setIcon(0,QIcon('./images/wifi.png'))



        #加载根节点的所有属性与子控件
        self._tree.addTopLevelItem(root)

        #TODO 优化3 给节点添加响应事件
        #self._tree.clicked.connect(self.onClicked)

        ###zj added 2
        self._textEdit = QTextEdit(self._left_side)
        self._textEdit.setContextMenuPolicy(Qt.CustomContextMenu)
        self._textEdit.customContextMenuRequested.connect(self.myListWidgetContext)
        #self._textEdit.customContextMenuRequested[QPoint].connect(self.myListWidgetContext)
        self._left_layout.addWidget(self._textEdit)

        ###


        #节点全部展开
        self._tree.expandAll()
        #self.setCentralWidget(self._tree)
        ###zj
        main_layout.addWidget(self._left_side, 0, 0)
        main_layout.setColumnStretch(0, 1)

        self.right_side = QWidget(main)
        self.right_layout = QVBoxLayout(self.right_side)
        # 设置stackedWidget
        self.stackedWidget = QStackedWidget()
        self.right_layout.addWidget(self.stackedWidget)

        # 设置第一个面板
        self.details_group = QGroupBox("Details", self.right_side)
        self.details_layout = QGridLayout(self.details_group)
        self._file_label = QLabel("<b>File:</b>")
        self.details_layout.addWidget(self._file_label, 0, 0)
        self._hash_label = QLabel("<b>Hash:</b>")
        self.details_layout.addWidget(self._hash_label, 1, 0)
        self.details_layout.setColumnStretch(0, 1)
        self._type_label = QLabel("<b>Type:</b>")
        self.details_layout.addWidget(self._type_label, 0, 1)
        ###
        self._date_btn = QPushButton()
        self._date_btn.setText('create')
        self.details_layout.addWidget(self._date_btn, 1, 1)
        self.details_layout.setColumnStretch(1, 1)
        # 设置第2个面板
        self.details_group2 = QGroupBox("Details2", self.right_side)
        self.details_layout2 = QGridLayout(self.details_group2)
        self._file_label2 = QLabel("<b>File2:</b>")
        self.details_layout2.addWidget(self._file_label2, 0, 0)
        self._date_btn2 = QPushButton()
        self._date_btn2.setText('jump back')
        self.details_layout2.addWidget(self._date_btn2, 0, 1)
        self.details_layout2.setColumnStretch(0, 1)

        # 将2个面板,加入stackedWidget
        self.stackedWidget.addWidget(self.details_group)
        self.stackedWidget.addWidget(self.details_group2)

        self._date_btn.clicked.connect(self.on_pushButton1_clicked)
        self._date_btn2.clicked.connect(self.on_pushButton2_clicked)
        main_layout.addWidget(self.right_side, 0, 1)
        main_layout.setColumnStretch(1, 2)

        self._databases_group = QGroupBox("Databases", self.right_side)
        self._databases_layout = QVBoxLayout(self._databases_group)
        self._databases_table = QTableWidget(0, 3, self._databases_group)
        labels = ("Name", "Date", "Ticks")
        self._databases_table.setHorizontalHeaderLabels(labels)
        horizontal_header = self._databases_table.horizontalHeader()
        horizontal_header.setSectionsClickable(False)
        horizontal_header.setSectionResizeMode(0, horizontal_header.Stretch)
        self._databases_table.verticalHeader().setVisible(False)
        self._databases_table.setSelectionBehavior(QTableWidget.SelectRows)
        self._databases_table.setSelectionMode(QTableWidget.SingleSelection)
        ##self._databases_table.itemSelectionChanged.connect(
            ##self._database_clicked
        ##)
        ##self._databases_table.itemDoubleClicked.connect(
           ##self._database_double_clicked
        ##)
        self._databases_layout.addWidget(self._databases_table)
        self.right_layout.addWidget(self._databases_group)

        buttons_widget = QWidget(self)
        buttons_layout = QHBoxLayout(buttons_widget)
        buttons_layout.addStretch()
        self._accept_button = QPushButton("Open", buttons_widget)
        self._accept_button.setEnabled(False)
        self._accept_button.clicked.connect(self.accept)
        cancel_button = QPushButton("Cancel", buttons_widget)
        cancel_button.clicked.connect(self.reject)
        buttons_layout.addWidget(cancel_button)
        buttons_layout.addWidget(self._accept_button)
        layout.addWidget(buttons_widget)

        # Ask the server for the list of projects
        ##d = self._plugin.network.send_packet(ListProjects.Query())
        ##d.add_callback(self._projects_listed)
        ##d.add_errback(self._plugin.logger.exception)
        
        ###zj add 3
        self.font = self._textEdit.font()
        self.fontSize = 15
        self.font.setPointSize(self.fontSize)
        self._textEdit.setFont(self.font)
        ###
    def on_pushButton1_clicked(self):
        self.stackedWidget.setCurrentIndex(1)
    def on_pushButton2_clicked(self):
        self.stackedWidget.setCurrentIndex(0)
    #自定义右键按钮
    def myListWidgetContext(self, *event_test):
        try:
            for i in event_test:
                print("event i: %s"%i)
        except:
            pass
        #print(event)
        popMenu = QMenu()
        popMenu.addAction(QAction(u'字体放大', self))
        popMenu.addAction(QAction(u'字体减小', self))
        popMenu.triggered[QAction].connect(self.processtrigger)
        popMenu.exec_(QCursor.pos())

    #右键按钮事件
    def processtrigger(self, q):
        text = self._textEdit.toPlainText()
        if not text.strip():
            return
        # 输出那个Qmenu对象被点击
        print("q is: %s"%q)
        print("text: %s"%text)
        if q.text() == "字体放大":
            self.fontSize += 2
            self.font.setPointSize(self.fontSize)
            self._textEdit.setFont(self.font)
            #self.fontSize += 1
        elif q.text() == "字体减小":
            self.fontSize -= 2
            self.font.setPointSize(self.fontSize)
            self._textEdit.setFont(self.font)
        #font = self._textEdit.font()      # lineedit current font
        #font.setPointSize(32)               # change it's size
        #self.texteditor1.setFont(font)  

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    mydialog = OpenDialog()
    mydialog.show()
    sys.exit(app.exec_())

在这里插入图片描述

在这里插入图片描述

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐