I have 3 three pictures shown in below:
How to let QLineEdit and QPushButton show in a column and style like this in a tableview in PyQt5?
I have the following three pictures shown in below,
I want to write a GUI which fulfill these feature by PyQt5:
- when click mouse one time, it will select this line, and also highlight this line1. just like digital 1 point to
after some seconds, at 'Click here to add a file' click mouse again one time, it will enter edit mode. just like digital 2 point to, a QLineEdit and a QPushButton '...' will display in the 2nd column. if I click '...', and pop up a File Selection Dialog, when I select a file, it will replace 'Click here to add a file' by file absolute path.
be careful: not double-click mouse enter into edit mode, it should be click mouse one time, some seconds later, click mouse again, will enter into edit mode. when I select a file which absolute path is very very long. I can see some char show behind QPushButton '...', it looks like QPushButton overlap on the right of QLineEdit.
- when step 2 is done, if continue to click mouse in other line, QLineEdit and QPushButton '...' in step 2 will disapper, like line 'VAR("myModelConer")
I have research 3 features many days, but cannot get my desire style. I will give my code in here, as a example, it is 2 rows and 2 columns. anybody could help me to modify and fullfil above 3 function.
Thanks in advance
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Delegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(Delegate, self).__init__(parent)
def createEditor(self, parent, option, index):
if index.column() == 0:
lineedit = QLineEdit("$woroot/1.scs",parent)
pushbutton = QPushButton("...", parent)
#lineedit = QLineEdit("..",self.parent())
#pushbutton = QPushButton("...", self.parent())
lineedit.index = [index.row(), index.column()]
pushbutton.index = [index.row(), index.column()]
h_box_layout = QHBoxLayout()
h_box_layout.addWidget(lineedit)
h_box_layout.addWidget(pushbutton)
h_box_layout.setContentsMargins(0, 0, 0, 0)
h_box_layout.setAlignment(Qt.AlignCenter)
widget = QWidget()
widget.setLayout(h_box_layout)
self.parent().setIndexWidget(
index,
widget
)
elif index.column() == 1:
combobox = QComboBox(parent)
combobox.addItems(section_list)
combobox.setEditable(True)
#combobox.editTextChanged.connect(self.commitAndCloseEditor)
return combobox
def setEditorData(self, editor, index):
text = index.model().data(index, Qt.DisplayRole)
print "setEditorData, text=", text
text = str(text)
i = editor.findText(text)
print "i=", i
if i == -1:
i = 0
editor.setCurrentIndex(i)
def setModelData(self, editor, model, index):
text = editor.currentText()
if len(text) >= 1:
model.setData(index, text)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
def commitAndCloseEditor(self):
editor = self.sender()
if isinstance(editor, (QTextEdit, QLineEdit,QSpinBox,QComboBox)):
self.commitData[QWidget].emit(editor)
self.closeEditor[QWidget].emit(editor)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
model = QStandardItemModel(4, 2)
tableView = QTableView()
tableView.setModel(model)
delegate = Delegate(tableView)
tableView.setItemDelegate(delegate)
section_list = ['w','c','h']
for row in range(4):
for column in range(2):
index = model.index(row, column, QModelIndex())
model.setData(index, (row + 1) * (column + 1))
tableView.setWindowTitle("Spin Box Delegate")
tableView.show()
sys.exit(app.exec_())