Results 1 to 14 of 14

Thread: Get text from QCombobox.

  1. #1
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Get text from QCombobox.

    Hello Anyone,

    I have qt-4.7

    I have a Mainwindow with QComboBox called diaCombobox.
    I have also severall QDialogs.
    Now i want to retrieve text from my MainWindow QComboBox (diaCombobox) en paste into my QDialog.
    How can i connect to my MainWindow from my QDialog to get the text from my MainWindow QCombobox (diaCombobox).

    mainwindow.h
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8. QComboBox *diaCombobox;
    9. etc..
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cc
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. diaCombobox = ui->diaCombobox;
    8. etc....
    To copy to clipboard, switch view to plain text mode 

    dialog.h
    [CODE]
    class SlotDialog : public QDialog
    {
    Q_OBJECT

    public:
    SlotDialog(QWidget *parent = 0);
    //int n;
    //QString ae;
    //QString vc;
    //QString voed;
    ~SlotDialog();

    public slots:
    void changed();

    private:
    Ui::SlotDialog *ui;
    MainWindow *parent;
    QTableWidget *slotTable;
    etc...
    [CODE]

    dialog.cc
    Qt Code:
    1. SlotDialog::SlotDialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::SlotDialog)
    4.  
    5. {
    6. ui->setupUi(this);
    7. connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));
    8. }
    9.  
    10. etc..
    To copy to clipboard, switch view to plain text mode 

    thanks in advanced.

  2. #2
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get text from QCombobox.

    this->ui->comboBox->currentText ();

    greetings

  3. #3
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get text from QCombobox.

    Hello,

    this->ui->comboBox->currentText ();

    This give my a error because in my QDialog i don't have a QCombobox.
    Only in my MainWindow.

    dialo.cc
    Qt Code:
    1. SlotDialog::SlotDialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::SlotDialog)
    4.  
    5. {
    6. ui->setupUi(this);
    7. connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));
    8. }
    9.  
    10. this->ui->comboBox->currentText (); this is pointing to my connect ().
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Get text from QCombobox.

    It really depends on exactly what you want to achieve and how the objects are create/related.

    Here are some options that rely only on standard C++:
    • Put a public getter method in your QMainWindow sub-class and call that from the dialog code
    • Put a public setter method in your QDialog sub-class and call that from the main window code at appropriate times.
    • Pass the value of the combo box into the dialog at construction


    This uses Qt:
    • Connect the "void currentIndexChanged ( const QString & text )" signal of the combo box to a receiving slot in the dialog.

  5. #5
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get text from QCombobox.

    you dont need to have any connect, you have the dialog and the combo box both created in the main if i got you right. Then when you create you dialog you "pass" this information to the constructor, for example your main will look like this:

    Qt Code:
    1. mainWin.show ();
    2. //....staff
    3. YourDialog dia = new YourDialog (parent, mainWin.getInfoFromCombo());
    4. dia.exec ();
    To copy to clipboard, switch view to plain text mode 

    that way you have the value of the comboBox and treat it as you like..

    yep my english sucks but i wanted to say something like what chris answered, lol

  6. #6
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get text from QCombobox.

    Hello,

    Thanks for the answer but this not what iám looking for.
    i don't want to use the main.cc for this application because i have severall dialogs to do the same thing.

    The dialogs will be called through the MainWindow with a button like this.
    mainwindow.cc
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. diaCombobox = ui->diaCombobox;
    8.  
    9. connect(ui->pbFullSlot, SIGNAL(clicked()), this, SLOT(fullslot())); // This is the pushbutton connection for my dialog.
    10. }
    11.  
    12. void MainWindow::fullslot(){
    13. SlotDialog dlg(this);
    14. if(dlg.exec() == QDialog::Accepted){ // The dialog popsup.
    15. }
    To copy to clipboard, switch view to plain text mode 

    Before i click on the pushbutton on the MainWindow i have some text in the Combobox on the MainWindow.
    That text i want to use in the dialog in a if statement like this.
    dialog.cc
    Qt Code:
    1. SlotDialog::SlotDialog(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::SlotDialog)
    4.  
    5. {
    6. ui->setupUi(this);
    7. connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));
    8. }
    9.  
    10. void SlotDialog::changed()
    11. {
    12. item = ui->slotTable->currentItem();
    13. if(item !=0){
    14. voed = item->text();
    15. }
    16.  
    17. if(ui->slotTable->currentColumn()== 1 ){
    18. QString ap = .......... // Here i want the text from the combobox from the Mainwindow.
    19. n = ap.toInt();
    20. n = n * 1.5;
    21.  
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 


    Thaks in advanced

  7. #7
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get text from QCombobox.

    as we said, if you need the text from the comboBox that is in mainWindow you need to do something like this

    Qt Code:
    1. //When you call this function you "pass" the combotext to the contructor of the dialog, change the constructor of SlotDialog
    2. void MainWindow::fullslot(){
    3. SlotDialog dlg(this, this->ui->comboBox->currentText ());
    4. if(dlg.exec() == QDialog::Accepted){
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. SlotDialog::SlotDialog(QWidget *parent, QString comboText) :
    2. QDialog(parent),
    3. ui(new Ui::SlotDialog)
    4.  
    5. {
    6. ui->setupUi(this);
    7. this.comboText = comboText;
    8. connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));
    9. }
    To copy to clipboard, switch view to plain text mode 

    you will need to create a variable comboText or whatever name you want to give it in your private of SlotDialog.h

    pretty neat? ah?

    then you just replace your dots for this

    Qt Code:
    1. if(ui->slotTable->currentColumn()== 1 ){
    2. QString ap = comboText; // Here i want the text from the combobox from the Mainwindow.
    3. n = ap.toInt();
    4. n = n * 1.5;
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

    hope it helps

  8. #8
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get text from QCombobox.

    Hello KillGabio,

    Thanks so far your explanation.
    I have only one error like this.

    In member function void MainWindow::fullslot()
    no matching function for call to SlotDialog::SlotDialog(MainWindow* const, QString)
    mainwindow.cc
    Qt Code:
    1. SlotDialog dlg(this, this->ui->diaCombobox->currentText());
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.

  9. #9
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get text from QCombobox.

    thats because you haven`t change your funtion in SlotDialog.h, the error is telling you this

    you have to look for the constructor and add something like this

    explicit SlotDialog(QWidget *parent = 0, QString = "no data"); //this is just an example
    this is a basic mistake u just need to try/error a little bit more

  10. #10
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get text from QCombobox.

    Hello KillGabio,

    I have still a error like this.
    In constructor SlotDialog::SlotDialog(Qwidget*, QString)
    request for member comboText in this of non-class type SlotDialog* const

    I have change in dialog.h the constructor like this.
    Qt Code:
    1. public:
    2. explicit SlotDialog(QWidget *parent = 0, QString ="");
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.

  11. #11
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get text from QCombobox.

    post all your code

  12. #12
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get text from QCombobox.

    Hello KillGabio,

    Here all my code.

    manwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui>
    5. #include <QtGui/QMainWindow>
    6.  
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20. public slots:
    21. void fullslot();
    22. private:
    23. Ui::MainWindow *ui;
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cc
    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include "mainwindow.h"
    4. #include "slotdialog.h"
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. setWindowIcon(QIcon(":/images/images/UOP_1.PNG"));
    12. connect(ui->pbFullSlot, SIGNAL(clicked()), this, SLOT(fullslot()));
    13.  
    14. Qt::WindowFlags flags;
    15. flags = Qt::Window | Qt::WindowMinimizeButtonHint;
    16. setWindowFlags( flags );
    17.  
    18. this->setStatusBar(false);
    19.  
    20. statusBar()->hide();
    21.  
    22. }
    23.  
    24. MainWindow::~MainWindow()
    25. {
    26. delete ui;
    27. }
    28.  
    29. void MainWindow::fullslot(){
    30. SlotDialog dlg(this, this->ui->diaCombobox->currentText());
    31. if(dlg.exec() == QDialog::Accepted){
    32. }
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 

    dialog.h
    Qt Code:
    1. #ifndef SLOTDIALOG_H
    2. #define SLOTDIALOG_H
    3.  
    4. #include <QDialog>
    5. #include <QtGui>
    6. #include "mainwindow.h"
    7.  
    8. namespace Ui {
    9. class SlotDialog;
    10. }
    11.  
    12. class SlotDialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit SlotDialog(QWidget *parent = 0, QString ="");
    18. ~SlotDialog();
    19. QString dia, voed;
    20.  
    21. public slots:
    22. void changed();
    23.  
    24. private:
    25. Ui::SlotDialog *ui;
    26. QString comboText;
    27. QTableWidget *slotTable;
    28. };
    29. #endif // SLOTDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    dialog.cc
    Qt Code:
    1. #include "slotdialog.h"
    2. #include "ui_slotdialog.h"
    3.  
    4.  
    5. SlotDialog::SlotDialog(QWidget *parent, QString comboText) :
    6. QDialog(parent),
    7. ui(new Ui::SlotDialog)
    8.  
    9. {
    10. ui->setupUi(this);
    11. this.comboText = comboText;
    12. connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));
    13.  
    14.  
    15. }
    16.  
    17. void SlotDialog::changed()
    18. {
    19. item = ui->slotTable->currentItem();
    20. if(item !=0){
    21. voed = item->text();
    22. }
    23. if(ui->slotTable->currentColumn()== 1 ){
    24. QString dia = combotext;
    25. etc.......
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    main.cc
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get text from QCombobox.

    mmm change this
    explicit SlotDialog(QWidget *parent = 0, QString ="");
    to this
    Qt Code:
    1. explicit SlotDialog(QWidget *parent = 0, QString comboText=" ");
    To copy to clipboard, switch view to plain text mode 

    oh and change this

    this.comboText = comboText
    for
    Qt Code:
    1. this->comboText= comboText
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Jan 2006
    Posts
    115
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Get text from QCombobox.

    Hello KillGabio,

    Thanks of lot this is what i'am looking for.
    It works perfect.
    Thanks for your time and advices.

  15. The following user says thank you to dragon for this useful post:

    KillGabio (31st January 2012)

Similar Threads

  1. Highlight an item in QCombobox by text color
    By AlekseyK in forum Qt Programming
    Replies: 6
    Last Post: 18th March 2019, 21:31
  2. Replies: 2
    Last Post: 28th April 2011, 14:43
  3. How to set Text Elide Mode for QComboBox
    By kalos80 in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2011, 08:42
  4. How to use rich text in a QComboBox?
    By aarunt1 in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2010, 20:20
  5. QComboBox with fixed text?
    By squidge in forum Qt Programming
    Replies: 0
    Last Post: 14th November 2009, 21:39

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
  •  
Qt is a trademark of The Qt Company.