Python As you continue to increase your value, I ask you a question.

Asked 2 years ago, Updated 2 years ago, 18 views

For example, raising the spinbox gui value. 50 150 200 250... The price should go up and down based on 50... (like the +- button on the shopping mall) I don't know if it's because I made the wrong chord, 50 100 200 400 800 1600 The value is added as a duplicate ㅜ 이걸 How can I do this? Is there a way to put an expression in order to express it correctly?

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class MyApp(QWidget):
    def __init__(self):
        super().__init__()

        self.MyApp()

    def MyApp(self):
        self.resize(500,300)
        self.tableData = [
            ['no1', 'name1', 'spinbox1', '100'],
            ['nn2', 'name2', 'spinbox2', '200'],
            ['no3', 'name3', 'spinbox3', '300'],
            ['no4', 'name4', 'spinbox4', '400'],
            ['no5', 'name5', 'spinbox5', '500']
        ]        
        self.qStandardItemModel = QStandardItemModel()   

        for i in range(len(self.tableData)):
            for j in range(len(self.tableData[0])):
                model = QStandardItem(self.tableData[i][j])              
                self.qStandardItemModel.setItem(i, j, model)   

        self.qTable = QTableView()
        self.qTable.setSelectionBehavior(QTableView.SelectRows)
        self.qTable.setEditTriggers(QTableView.NoEditTriggers)
        self.qTable.setModel(self.qStandardItemModel)   

        for i in range(5):
            self.qSpinbox = QSpinBox()    
            index = self.qTable.model().index(i, 2)
            self.qTable.setIndexWidget(index, self.qSpinbox)

        self.qSpinbox.valueChanged.connect(self.valueChanged)

        vbox = QVBoxLayout(self)
        vbox.addWidget(self.qTable)
        self.setLayout(vbox)  

    def valueChanged(self):
        print(self.qSpinbox.value())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    ex.show()
    sys.exit(app.exec_())

Here's the example code. If you press the We button on the spin box, the fourth column value is added I'd like to find something that comes out when I press the button below. Putting in and taking out data is a model I get it, but when you press the upper button and the lower button, Should I say we're sharing signals? I'd like to know how to separate it and use a copy of the variable I'm asking for your help because it's not possible to implement it separately. If you press the upper button, you can add it, and if you press the lower button, it seems very simple, but it is very difficult because the valueChanged.connect signal is one In c++, I think I saw that it was implemented as a case statement through a value of 1 and -1. This part is not even in the official qt

python

2022-09-22 13:34

2 Answers

Questioner's code connects only the last spinbox.

You need to connect to each spinbox. You can also increase it by applying singlestep.

for i in range(5):
    self.qSpinbox = QSpinBox()    
    self.qSpinbox.setMinimum(0)
    self.qSpinbox.setMaximum(5000)
    self.qSpinbox.setSingleStep(int(self.tableData[i][3]))
    # # self.qSpinbox.setSingleStep(int(self.qTable.model().item(i, 3).Text())) # When you get it from the table
    index = self.qTable.model().index(i, 2)
    self.qTable.setIndexWidget(index, self.qSpinbox)
    self.qSpinbox.valueChanged.connect(self.valueChanged) # Each widget must have a connection.

You can find out which widget you called through sender. That is, the code below gets the value of the called spinbox.

def valueChanged(self):
        print(self.sender().value())


2022-09-22 13:34

In the past, I wrote an example of Qt as if I were posting a Qt question.

You can look at the spinbox part below carefully below.

import sys

from PyQt5 import Qt, QtCore, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QSpinBox


class Spindemo(QWidget):
    '''
    QSpinBox Sample
    '''
    def __init__(self):
        super(Spindemo, self).__init__()
        vbox = QVBoxLayout()

        self.label = QLabel()
        vbox.addStretch(1)
        vbox.addWidget(self.label)

        self.spinBox = QSpinBox(self)
        self.spinBox.setMinimum(0) #Set the minimum value of the spinbox
        self.spinBox.setMaximum (1000) # Setting the maximum value of the spin box
        self.spinBox.setSingleStep(50) # Increase or subtract the allocation value per step of the spin box by 50

        vbox.addStretch(1)
        vbox.addWidget(self.spinBox)
        self.spinBox.valueChanged.connect(self.valuechange)

        self.setLayout(vbox)
        self.setGeometry(400, 200, 200, 50)

    def valuechange(self):
        self.label.setText("current value: " + str(self.spinBox.value()))")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Spindemo()
    window.show()
    app.exec_()


2022-09-22 13:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.