Results 1 to 3 of 3

Thread: How to get value from QCombobox which is configured as QWidget in QTableWidget ??

  1. #1
    Join Date
    Nov 2017
    Posts
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default How to get value from QCombobox which is configured as QWidget in QTableWidget ??

    Hello All,

    I have created a Qcombobox as a Qwidget in a QtableWidget and it is working fine, The below code i have used for creating combobox in table widget.

    Qt Code:
    1. QWidget *pWidget1 = new QWidget();
    2. QComboBox *ipcombo = new QComboBox;
    3. QHBoxLayout * pLayout1 = new QHBoxLayout(pWidget1);
    4. pLayout1->addWidget(ipcombo);
    5. pLayout1->setAlignment(Qt::AlignCenter);
    6. pLayout1->setContentsMargins(0,0,0,0);
    7. pWidget1->setLayout(pLayout1);
    8. ipcombo->addItem("YES");
    9. ipcombo->addItem("NO");
    10. ui->tableWidget->setCellWidget(1,3,pWidget1);
    11. connect(ipcombo, SIGNAL(currentIndexChanged(QString)),this,SLOT(on_currentIndexChanged(QString)));
    12. index++;
    To copy to clipboard, switch view to plain text mode 

    now the problem is i couldn't get the data out of the combobox which is assigned as a cellwidget. the below code only am using to get the data. if the button is clicked the SIGSEGV segmentation fault error is happening.

    Qt Code:
    1. void MainWindow::on_submitbtn_2_clicked()
    2. {
    3. QStringlist ComboData;
    4. QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget_2->cellWidget(row,3));
    5. Combodata<< myCB->currentText();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Kindly help me
    thanks in advance..
    Last edited by high_flyer; 21st December 2017 at 10:25. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to get value from QCombobox which is configured as QWidget in QTableWidget ??

    f the button is clicked the SIGSEGV segmentation fault error is happening.
    That is becase probably the returned QComboBox pointer is null:
    This should not crash:
    Qt Code:
    1. void MainWindow::on_submitbtn_2_clicked()
    2. {
    3. QStringlist ComboData;
    4. QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget_2->cellWidget(row,3));
    5. if(myCB){
    6. Combodata<< myCB->currentText();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,057
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How to get value from QCombobox which is configured as QWidget in QTableWidget ??

    Well, there is a small difference between this:
    ui->tableWidget->setCellWidget(1,3,pWidget1);
    and this:
    QComboBox *myCB = qobject_cast<QComboBox*>(ui->tableWidget_2->cellWidget(row,3));
    which could easily case a crash when you try to use the NULL pointer returned by the code above.
    <=== 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. Access QComboBox inside QTableWidget
    By phil333 in forum Qt Programming
    Replies: 4
    Last Post: 27th February 2017, 16:29
  2. QTableWidget and QCombobox
    By Stanfillirenfro in forum Qt Programming
    Replies: 2
    Last Post: 30th December 2013, 17:58
  3. Again QTableWidget and QComboBox delegate
    By Aki-Matti in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2008, 13:40
  4. Adding QComboBox to QTableWidget very slow
    By munna in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2006, 15:45
  5. QComboBox inside QTableWidget
    By campana in forum Qt Programming
    Replies: 7
    Last Post: 20th March 2006, 17:22

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.