How to display QTableWidget selected row in a qLineEdit of the same class?
-
Please I will need your help. I am not that proficient with QTableWidget and honestly need your help with proper guidance.
Before I came here, I have made several research but to no avail. I have also studied the Qt documentation but don't understand it.I have basic knowledge of Python and pyqt5 and have written some projects anyway, my problem is with QTableWidget.
I am writing a program and in one of the windows, a click of a button loads a window with a qtablewidget in it. This qtablewidget displays retrieved records from the database.
Please I need a line of code that can make any selected row of the qTablewidget to be displayed in a particular lineEdit in the same class with the qTableWidget.
I would prefer a code example as an illustration. Please. In the attached photo, you can see the selectedRow label and its QLineEdit which is readOnly, please I want the content of the selectedRow in the qtablewidget to display in that QLineEdit.Kindly help me with code based on my code[ATTACH=CONFIG]13697[/ATTACH]
The below is an extract from my program:
[CODE] displaylb = QLabel("Selected row") self.displaytx = QLineEdit() self.displaytx.setReadOnly(True) self.tab = QtWidgets.QTableWidget(self) Headerlist = [this contains the columns title in the qTablewidget] self.tab.setRowCount(100000) self.tab.setColumnCount(15) self.tab.setHorizontalHeaderLabels(Headerlist) self.tab.horizontalHeader().setStyleSheet("::section{background-color: blue}") self.tab.setGeometry(150, 200, 1700, 1700) self.tab.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents) self.tab.resizeColumnsToContents() #self.tab.setColumnWidth(0,150) self.tab.setSelectionBehavior(QAbstractItemView.SelectRows) self.tab.horizontalHeader().setSectionResizeMode( QHeaderView.Stretch) # this makes the columns fit in the widget self.tab.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers) self.tab.cellDoubleClicked.connect(self.tabClick)
[/CODE]
-
Hi,
Do you mean you want to build a string of the row and then put that in your QLineEdit ?
For example in a slot connected to the currentCellChanged signal ?
-
-
@CEO
You can do all this work yourself with your own line edits, but this is not the best/easiest/most efficient way to go. Instead Qt offers you two ways which (I think) are better:-
You can make it so the
QTableWidget
is editable: the user clicks into a cell and edits it there. No separate line edits. If you like it that way. -
You can use QDataWidgetMapper to "map" a whole series of separate, individual appropriate edit widgets to the columns in the selected row of a model. If you like it that way.
-
-
@CEO said in How to display QTableWidget selected row in a qLineEdit of the same class?:
Can you write a code for that plz?
Are you serious?
I want the displayed values in their corresponding and different line edits.
Then I would suggest you use the
QDataWidgetMapper
. -
-
@JonB now I discovered you are either hoarding knowledge or might not have done any work on the question. I checked different contributions in some thread now and saw some members explicitly writing out lines of codes. One of them even helped me greatly and I just have to call him a pro. If you know, there's no need hoarding it.
Anyway, thanks for the time.
-
@CEO said in How to display QTableWidget selected row in a qLineEdit of the same class?:
@JonB now I discovered you are either hoarding knowledge or might not have done any work on the question.
Yes, you're right. I have nothing better to do than lie to questioners about what I know and they should do, and I don't spend enough of my time writing the code demanded to save them time.
Here is an extract from my usage of the
QDataWidgetMapper
which I know nothing about from a sample project of mine. I'm sorry if my variables/widgets/model are not the same as yours. Also here I happen to be binding toQSpinBox
es, let me know if I need to change it toQLineEdit
s for you.void SectorRegisters::initDataWidgetMapper() { // set up the `QDataWidgetMapper` this->dwm = new QDataWidgetMapper(this); // set model (this->sectorsModel is the model being used, in my case it's a `QStandardItemModel`) SectorsModel *sectorsModel = this->sectorsModel; dwm->setModel(sectorsModel); // Vertical => widget mapped to row dwm->setOrientation(Qt::Vertical); // current index is always column #0 dwm->setCurrentIndex(0); // Region mappings dwm->addMapping(ui->spinRegionSocialState, SectorsModel::RegionSocialState); dwm->addMapping(ui->spinRegionGoodAreas, SectorsModel::RegionGoodAreas); dwm->addMapping(ui->spinRegionPoorAreas, SectorsModel::RegionPoorAreas); dwm->addMapping(ui->spinRegionCash, SectorsModel::RegionCash); // Here there are many further widget<->model-column mappings // ... }
You're welcome.