如何通过单击QPushButton

如何解决如何通过单击QPushButton

我有一个带有两个标签的界面:在第一个标签中,我要求用户输入参数,在第二个标签中,我要打印以下QTableWidget。

所以基本上在第一个选项卡上我有一个QPushButton,我将其称为process,通常,当我按下它时,我想将信息发送到第二个选项卡。

现在,我刚刚尝试显示一个带有QTableWidget和良好参数的新窗口:

class Parameters(QWidget):
  
    def __init__(self):
        super(Parameters,self).__init__()

        self.matrixsize = QLineEdit()
        bouton = QPushButton("define matrix_size")
        bouton.clicked.connect(self.appui_bouton)
        self.halfmatrix = QCheckBox()
        self.halfmatrix.toggled.connect(self.on_checked)

   
        self.define_matrix_size = QGroupBox('Define Parameters')
        layout = QGridLayout()
        layout.addWidget(self.matrixsize,1,)
        layout.addWidget(bouton,1)
        layout.addWidget(QLabel('select half size mode'
                                ),1)
        layout.addWidget(self.halfmatrix,1)
        self.define_matrix_size.setLayout(layout)

        process = QPushButton('process')
        process.clicked.connect(self.process)

        self.matrix = QTableWidget()
        self.layout = QGridLayout()
        self.layout.addWidget(self.define_matrix_size)
  
        self.layout.addWidget(matrix)
        self.layout.addWidget(process)

        self.setLayout(self.layout)

    def matrix_size(self):
        if self.matrixsize.text() == "":
            return 0

        else:
            return int(self.matrixsize.text())

       def appui_bouton(self):
        taille = self.matrixsize()
        self.matrix.deleteLater()
        if self.halfmatrix.isChecked():
            self.on_checked()

        else:
            self.matrix = QTableWidget()
            self.matrix.setColumnCount(taille)
            self.matrix.setRowCount(taille)
            self.layout.addWidget(self.matrix)
            self.update()
            self.setLayout(self.layout)

  


    def keyPressEvent(self,qKeyEvent):
        print(qKeyEvent.key())
        if qKeyEvent.key() == Qt.Key_Return or qKeyEvent.key() == Qt.Key_Enter:
            self.appui_bouton()
        else:
            super().keyPressEvent(qKeyEvent)

    def on_checked(self):
        taille = self.matrixsize()
        if taille == 0:
            pass

        else:
            if self.halfmatrix.isChecked():


                size = int(taille / 2)
                self.matrix.deleteLater()
                self.matrix = QTableWidget()
                self.matrix.setColumnCount(size)
                self.matrix.setRowCount(size)
                self.layout.addWidget(self.matrix,3,20,4)
                self.update()

                self.setLayout(self.layout)

            else:
                self.appui_bouton()

    def process (self):
        
        layout = QHBoxLayout()

        test = self.matrix
        test.setLayout(layout)
        test.show()

因此,为了澄清我的意思:我有一个窗口,在该窗口上您可以获取一些参数(大小,...),当您选择这些参数时,假设您采用matrixsize == 5,则有一个5x5的表是添加到窗口。该表格可以通过拖放系统由其他参数填充(在代码上剪切它们)。 因此,现在我有了一个已建好的表,我希望能够通过单击“处理”按钮来仅用该表打开一个新窗口。 所以我不想要动态表,我只想要一个具有相同属性的表(例如,如果矩阵启用了dragonly enable,那么新矩阵应该具有相同的属性)。我想将所有信息保留在单元格中

我希望我已经足够清楚了,这是我第一次问问题(经过多次阅读当然的答案^^) 感谢您的回答和建议!

解决方法

您可以只创建一个没有父级的新QTableWidget(这使其成为顶层窗口),然后显示它:

class Parameters(QWidget):
    # ...
    def process(self):
        rows = self.matrix.rowCount()
        columns = self.matrix.columnCount()
        self.newTable = QTableWidget(rows,columns)
        for row in range(rows):
            for column in range(columns):
                source = self.matrix.item(row,column)
                if source:
                    self.newTable.setItem(row,column,QTableWidgetItem(source))
        self.newTable.show()

请注意,我将新表创建为实例属性。这样可以避免垃圾回收(如果它是一个局部变量)(导致小部件显示并立即消失),但不幸的是,如果再次单击流程按钮并且已经存在一个窗口,则会将其删除并用新窗口“覆盖”。如果要同时拥有更多的过程窗口,可以将它们添加到列表中:

class Parameters(QWidget):
    def __init__(self):
        super(Parameters,self).__init__()
        # ...
        self.processTables = []

    def process(self):
        rows = self.matrix.rowCount()
        columns = self.matrix.columnCount()
        # note that now "newTable" is *local*
        newTable = QTableWidget(rows,columns)
        self.processTables.append(newTable)
        # ...

关于您的代码的一些建议:

  • 每次更改大小时,绝对不需要创建新表;只需在现有的setRowCountsetColumnCount上使用,如果不想保留以前的值,请使用clear();
  • 不要使用两个功能几乎相同的功能(appui_boutonon_checked)并互相调用,而只需使用一个可以同时检查两个方面的功能即可。
  • 不要不必要地调用update():当您更改窗口小部件的属性(或将新的窗口小部件添加到布局中)时,update已经被调用;尽管这不是一个实际问题(Qt在发生实际更新时自动管理 ,并在必要时避免重新绘制),但调用它只会给您的代码添加不必要的噪音;
  • 将小部件添加到网格布局时要格外小心(我指的是on_checked上的代码):如果不需要,请不要使用rowSpan和columnSpan;同样,使用高值是完全没有用的,因为该行中没有其他小部件,并且该布局中实际上只有一列;另外,请勿再次致电setLayout()
  • 如果需要数字值,请使用QSpinBox,而不要使用QLineEdit。

可以很容易地重写用于更新现有表的功能,您应该将按钮复选框都连接到该按钮:

class Parameters(QWidget):
    def __init__(self):
        super(Parameters,self).__init__()
        self.matrixsize = QSpinBox()
        bouton = QPushButton("define matrix_size")
        bouton.clicked.connect(self.appui_bouton)
        self.halfmatrix = QCheckBox()
        self.halfmatrix.toggled.connect(self.appui_bouton)
        # ...

    def appui_bouton(self):
        taille = self.matrixsize.value()
        if self.halfmatrix.isChecked():
            taille //= 2
        if not taille:
            return
        self.matrix.setColumnCount(taille)
        self.matrix.setRowCount(taille)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-