Results 1 to 2 of 2

Thread: Displaying selectedRow text in a QLineEdit

  1. #1
    Join Date
    Aug 2021
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Displaying selectedRow text in a QLineEdit

    Hello,

    Please I will need you 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 codetabw.jpg

    The below is an extract from my program:




    Qt Code:
    1. [ATTACH=CONFIG]13697[/ATTACH]displaylb = QLabel("Selected row")
    2. self.displaytx = QLineEdit()
    3. self.displaytx.setReadOnly(True)
    4. self.tab = QtWidgets.QTableWidget(self)
    5. Headerlist = [this contains the columns title in the qTablewidget]
    6. self.tab.setRowCount(100000)
    7. self.tab.setColumnCount(15)
    8. self.tab.setHorizontalHeaderLabels(Headerlist)
    9. self.tab.horizontalHeader().setStyleSheet("::section{background-color: blue}")
    10. self.tab.setGeometry(150, 200, 1700, 1700)
    11. self.tab.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
    12. self.tab.resizeColumnsToContents()
    13. #self.tab.setColumnWidth(0,150)
    14. self.tab.setSelectionBehavior(QAbstractItemView.SelectRows)
    15. self.tab.horizontalHeader().setSectionResizeMode(
    16. QHeaderView.Stretch) # this makes the columns fit in the widget
    17. self.tab.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers)
    18. self.tab.cellDoubleClicked.connect(self.tabClick)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,246
    Thanks
    304
    Thanked 866 Times in 853 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Displaying selectedRow text in a QLineEdit

    I do not know enough Python to give you code, but since this sounds like a homework assignment you should write the code yourself. This is what you need to do:

    - connect the QTableWidget::itemSelectionChanged() signal to a slot in your class

    - in that slot, call QTableWidget::selectedItems(). This will give you a list of the QTableWidgetItem pointers that are the selection. Since you are selecting the entire row, then this will be a list of all of the cells in that row.

    - pick the QTableWidgetItem for the column you want and use QTableWidgetItem::text() to retrieve the QString.

    - call QLineEdit::setText()
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. QlineEdit with constant text and editable text fields
    By arunpk in forum Qt Programming
    Replies: 6
    Last Post: 17th November 2015, 13:38
  2. Replies: 1
    Last Post: 6th September 2015, 12:09
  3. Replies: 1
    Last Post: 11th March 2014, 12:20
  4. displaying text
    By me_here_me in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2007, 11:48
  5. Displaying Text Quickly
    By taylor34 in forum Qt Programming
    Replies: 6
    Last Post: 2nd March 2006, 15:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.