PDA

View Full Version : Get text from QCombobox.



dragon
30th January 2012, 18:27
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


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();
QComboBox *diaCombobox;
etc..


mainwindow.cc


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

diaCombobox = ui->diaCombobox;
etc....


dialog.h


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
[CODE]
SlotDialog::SlotDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SlotDialog)

{
ui->setupUi(this);
connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));
}

etc..


thanks in advanced.

KillGabio
30th January 2012, 19:10
this->ui->comboBox->currentText ();

greetings

dragon
30th January 2012, 19:48
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


SlotDialog::SlotDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SlotDialog)

{
ui->setupUi(this);
connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));
}

this->ui->comboBox->currentText (); this is pointing to my connect ().

ChrisW67
30th January 2012, 21:00
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.

KillGabio
30th January 2012, 21:04
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:



mainWin.show ();
//....staff
YourDialog dia = new YourDialog (parent, mainWin.getInfoFromCombo());
dia.exec ();


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

dragon
31st January 2012, 06:51
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


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

diaCombobox = ui->diaCombobox;

connect(ui->pbFullSlot, SIGNAL(clicked()), this, SLOT(fullslot())); // This is the pushbutton connection for my dialog.
}

void MainWindow::fullslot(){
SlotDialog dlg(this);
if(dlg.exec() == QDialog::Accepted){ // The dialog popsup.
}


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



SlotDialog::SlotDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SlotDialog)

{
ui->setupUi(this);
connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));
}

void SlotDialog::changed()
{
QTableWidgetItem *item = new QTableWidgetItem();
item = ui->slotTable->currentItem();
if(item !=0){
voed = item->text();
}

if(ui->slotTable->currentColumn()== 1 ){
QString ap = .......... // Here i want the text from the combobox from the Mainwindow.
n = ap.toInt();
n = n * 1.5;

}
}



Thaks in advanced

KillGabio
31st January 2012, 15:39
as we said, if you need the text from the comboBox that is in mainWindow you need to do something like this


//When you call this function you "pass" the combotext to the contructor of the dialog, change the constructor of SlotDialog
void MainWindow::fullslot(){
SlotDialog dlg(this, this->ui->comboBox->currentText ());
if(dlg.exec() == QDialog::Accepted){
}




SlotDialog::SlotDialog(QWidget *parent, QString comboText) :
QDialog(parent),
ui(new Ui::SlotDialog)

{
ui->setupUi(this);
this.comboText = comboText;
connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));
}


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


if(ui->slotTable->currentColumn()== 1 ){
QString ap = comboText; // Here i want the text from the combobox from the Mainwindow.
n = ap.toInt();
n = n * 1.5;

}

hope it helps

dragon
31st January 2012, 17:19
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


SlotDialog dlg(this, this->ui->diaCombobox->currentText());


Thanks in advance.

KillGabio
31st January 2012, 18:01
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 :D

dragon
31st January 2012, 18:32
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.


public:
explicit SlotDialog(QWidget *parent = 0, QString ="");


Thanks in advance.

KillGabio
31st January 2012, 18:35
post all your code

dragon
31st January 2012, 19:18
Hello KillGabio,

Here all my code.

manwindow.h


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui>
#include <QtGui/QMainWindow>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:
void fullslot();
private:
Ui::MainWindow *ui;
QTableWidgetItem *item;
};

#endif // MAINWINDOW_H


mainwindow.cc


#include <QtGui>
#include <QtCore>
#include "mainwindow.h"
#include "slotdialog.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/images/images/UOP_1.PNG"));
connect(ui->pbFullSlot, SIGNAL(clicked()), this, SLOT(fullslot()));

Qt::WindowFlags flags;
flags = Qt::Window | Qt::WindowMinimizeButtonHint;
setWindowFlags( flags );

this->setStatusBar(false);

statusBar()->hide();

}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::fullslot(){
SlotDialog dlg(this, this->ui->diaCombobox->currentText());
if(dlg.exec() == QDialog::Accepted){
}

}


dialog.h


#ifndef SLOTDIALOG_H
#define SLOTDIALOG_H

#include <QDialog>
#include <QtGui>
#include "mainwindow.h"

namespace Ui {
class SlotDialog;
}

class SlotDialog : public QDialog
{
Q_OBJECT

public:
explicit SlotDialog(QWidget *parent = 0, QString ="");
~SlotDialog();
QString dia, voed;

public slots:
void changed();

private:
Ui::SlotDialog *ui;
QString comboText;
QTableWidget *slotTable;
};
#endif // SLOTDIALOG_H


dialog.cc


#include "slotdialog.h"
#include "ui_slotdialog.h"


SlotDialog::SlotDialog(QWidget *parent, QString comboText) :
QDialog(parent),
ui(new Ui::SlotDialog)

{
ui->setupUi(this);
this.comboText = comboText;
connect(ui->slotTable, SIGNAL(cellClicked(int,int)), this, SLOT(changed()));


}

void SlotDialog::changed()
{
QTableWidgetItem *item = new QTableWidgetItem();
item = ui->slotTable->currentItem();
if(item !=0){
voed = item->text();
}
if(ui->slotTable->currentColumn()== 1 ){
QString dia = combotext;
etc.......
}
}


main.cc


#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

KillGabio
31st January 2012, 19:22
mmm change this

explicit SlotDialog(QWidget *parent = 0, QString ="");

to this


explicit SlotDialog(QWidget *parent = 0, QString comboText=" ");


oh and change this


this.comboText = comboText

for

this->comboText= comboText

dragon
31st January 2012, 20:50
Hello KillGabio,

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