PDA

View Full Version : send signal from QCombobox



raphaelf
27th February 2006, 23:26
QT:4.1.1

Hi everybody!
I am trying to call a function from my combobox but i was not able. I tryied: all signals but it not works.
It would be perfect if i click on my combobox and send a signal, or call my function...
Have somebody a idea how could i call my function by clicking my combobox for example.

My Combobox is per default empty. And my idea is that when you click the combobox a query will started and values should be added to this combobox

I tried this:


connect(ui.sprache_cb, SIGNAL( activated()), this, SLOT(selectSprache()));
connect(ui.sprache_cb, SIGNAL( clicked()), this, SLOT(selectSprache()));
connect(ui.sprache_cb, SIGNAL( highlighted()), this, SLOT(selectSprache()));

Have i a chance to solve it?

high_flyer
28th February 2006, 00:20
Run the app from the command prompt and see if you get any warnings.

Chicken Blood Machine
28th February 2006, 03:00
QT:4.1.1

Hi everybody!
I am trying to call a function from my combobox but i was not able. I tryied: all signals but it not works.
It would be perfect if i click on my combobox and send a signal, or call my function...
Have somebody a idea how could i call my function by clicking my combobox for example.

My Combobox is per default empty. And my idea is that when you click the combobox a query will started and values should be added to this combobox

I tried this:


connect(ui.sprache_cb, SIGNAL( activated()), this, SLOT(selectSprache()));
connect(ui.sprache_cb, SIGNAL( clicked()), this, SLOT(selectSprache()));
connect(ui.sprache_cb, SIGNAL( highlighted()), this, SLOT(selectSprache()));

Have i a chance to solve it?


So presumably, you start with an empty combobox and you want to react to when the mouse is clicked on it and the combo "drops down"?

or did I misunderstand?

raphaelf
28th February 2006, 09:34
Hi Chicken Blood!

Yes the combobox should be empty at the beginning..(After the first query started the combobox will not be empty)
And allways when i click my combobox i should see the newest values from the database.
I dont now wich signal exactly i should use, i just must be shure that on click or activated the combobox, a query should be startet to be sure that the combobox contains the newest values and should be able to choose one of them..

zlatko
28th February 2006, 10:25
Just idea...use event filter for catch event QEvent::Show for object yourCmb->listBox()

jpn
28th February 2006, 10:32
activated and highlighted signals are emitted only when combo box contains items than can be activated or highlighted.

Try this:
Inherit QComboBox and override:

void QWidget::focusInEvent ( QFocusEvent * event ) [virtual protected]
When the combo box receives focus for the first time (when it's empty), you could emit a signal stating it needs to be populated.

raphaelf
28th February 2006, 10:47
Hi everybody,

Zlatko and Jpn, the both idea are nice..
Where could i find examples?I need more Information how to implement it..
Thanx

Mad Max
28th February 2006, 10:49
Do try to create the successor from the QComboBox class. Such as:

class ComboBoxModify : public QComboBox
{
Q_OBJECT
public:
ComboBoxModify(QWidget * pParent = 0)
: QComboBox(pParent) { ; };
public slots:
void setCurrentIndex ( int index )
{
QComboBox::setCurrentIndex( index );
emit QComboBox::activated ( index );
};
};

zlatko
28th February 2006, 10:52
// in header of your form
protected:
bool eventFilter( QObject *o, QEvent *e );

// in constructor your form
installEventFilter(yourCmb->listBox());

// then
bool yourForm::eventFilter( QObject *o, QEvent *e )
{
if ( e->type() == QEvent::Show )
{
yourMethod();
return TRUE; // eat event
} else
{
// standard event processing
return FALSE;
}
}

raphaelf
28th February 2006, 11:09
Hi Zlatko,

Thanks for your fast Support!
I get a error Message:


test.cpp:16: error: 'class QComboBox' has no member named 'listBox'


Could be that you mean another member?I think listbox was in Qt3 possible..


MAD MAX: Thanks for your example too, i have see just now(you reply in 120 seconds)..I will try all sendet examples ;)

zlatko
28th February 2006, 11:32
Yes qt4 hasnt..i think you must use comboboxes method view()

raphaelf
28th February 2006, 12:22
Hi,

I get No Errors now...But if i click my ComboBox my Function "selectSprache()" will not be called, Can somebody see why?



#include "test.h"

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QTextEdit>
#include <QStatusBar>
#include <QAbstractItemView>
#include <QEvent>




MainWindow::MainWindow()

{
ui.setupUi(this);
installEventFilter(ui.sprache_cb->view());

connect(ui.actionverbinden, SIGNAL(triggered()), this, SLOT(verbinden()));
connect(ui.speichern_btn, SIGNAL(clicked()), this, SLOT(selectSprache()));
}

bool MainWindow::verbinden()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("pcpsr5");
db.setDatabaseName("DRIVER={SQL Server};SERVER=pcpsr5;DATABASE=inventar;UID=sa;PWD =");
db.setUserName("sa");
db.setPassword("");
if(!db.open())
{
QMessageBox::information(this,"",db.lastError().text());
return false;
}
else
return true;
}
void MainWindow::selectSprache()
{
/*
QSqlQuery select (" select sprache from sprache_tbl");
while(select.next())
{
QString sprachen = select.value(0).toString();
ui.sprache_te->insertPlainText(sprachen + " ");
}
*/
QMessageBox::information(this,"","");
}

bool MainWindow::eventFilter( QObject *o, QEvent *e )
{
if ( e->type() == QEvent::Show )
{
selectSprache();
return TRUE;
}
else
return FALSE;

}




#include "ui_mainwindow.h"



class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();
QStatusBar *statusbar;


public slots:
bool verbinden();
void selectSprache();


private:
Ui::MainWindow ui;

protected:
bool eventFilter( QObject *o, QEvent *e );



};

zlatko
28th February 2006, 12:35
Stop...its bad idea catch this event if listview is empty :o .
Has your combo is editable?
If no try use event filter for object combobox and something event alikeQEvent::MouseButtonPress

sorry for my thoughtlessness

jpn
28th February 2006, 12:35
The way event filters are used is:
monitoredObj->installEventFilter(filterObj)

so in your case it would be:
ui.sprache_cb->installEventFilter(this)

If you want to populate your combo box due to a click, you might wanna consider changing QEvent::Show to some other event. Combo box receives the show event during the startup of the application..

zlatko
28th February 2006, 12:39
The way event filters are used is:
monitoredObj->installEventFilter(filterObj)

so in your case it would be:
ui.sprache_cb->installEventFilter(this)

If you want to populate your combo box due to a click, you might wanna consider changing QEvent::Show to some other event. Combo box receives the show event during the startup of the application..

he try catch Event::Show from comboboxes listview

jpn
28th February 2006, 12:44
he try catch Event::Show from comboboxes listview

Ok, then there is a problem that the list never gets shown as it's empty..

Edit: as you seem to have already noticed :) Nevermind...

raphaelf
28th February 2006, 13:10
Hi Guys..
like that it works, but just if i dobleclick my combobox :(


#include "test.h"

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QTextEdit>
#include <QStatusBar>
#include <QAbstractItemView>
#include <QEvent>




MainWindow::MainWindow()

{
ui.setupUi(this);
ui.sprache_cb->installEventFilter(this);


connect(ui.actionverbinden, SIGNAL(triggered()), this, SLOT(verbinden()));


}

bool MainWindow::verbinden()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("pcpsr5");
db.setDatabaseName("DRIVER={SQL Server};SERVER=pcpsr5;DATABASE=inventar;UID=sa;PWD =");
db.setUserName("sa");
db.setPassword("");
if(!db.open())
{
QMessageBox::information(this,"",db.lastError().text());
return false;
}
else
return true;
}
void MainWindow::selectSprache()
{
ui.sprache_cb->clear();
ui.sprache_cb->insertItem(0,"");
QSqlQuery select (" select sprache from sprache_tbl");
while(select.next())
{
QString sprachen = select.value(0).toString();
ui.sprache_cb->insertItem(1, sprachen);
}

}

bool MainWindow::eventFilter( QObject *o, QEvent *e )
{
if ( e->type() == QEvent::MouseButtonPress )
{
selectSprache();
return TRUE;
}
else
return FALSE;

}

zlatko
28th February 2006, 13:48
try release event use

raphaelf
28th February 2006, 14:02
Hi!
I get new values just if i click twice..and on release the small button right from the combobox stay active (like a toogle button) until a click again ;(


QEvent::MouseButtonRelease


May be its not the right way to send a Signal from the ComboBox..

raphaelf
28th February 2006, 14:13
Hi,
"QEvent::FocusIn" looks to work but i have a symptom..if i ordered my query the results are interchanged
combo: x,f,a
Query Analyzer: a,f,x

"select sprache from sprache_tbl order by sprache"
:confused:

jpn
28th February 2006, 14:17
Hi!
I get new values just if i click twice..and on release the small button right from the combobox stay active (like a toogle button) until a click again ;(

Supposedly you want to populate the combo box only once, so maybe you could remove the event filter right after receiving the first appropriate event.

Anyhow, I have included an example of the solution I suggested earlier...

jpn
28th February 2006, 14:23
Hi,
"QEvent::FocusIn" looks to work but i have a symptom..if i ordered my query the results are interchanged
combo: x,f,a
Query Analyzer: a,f,x

"select sprache from sprache_tbl order by sprache"
:confused:

Use "order by sprache desc" in your sql statement or add items to the combo box in reverse order, see last() (http://doc.trolltech.com/4.1/qsqlquery.html#last) and previous() (http://doc.trolltech.com/4.1/qsqlquery.html#previous).

raphaelf
28th February 2006, 15:18
Hi jpn!
Thank you very much for your spended time..I have downloaded the example an i will use "desc" for my Query..

Thanks all for the best support!