Qt-interest Archive, July 2007
QSignalMapper questions
Message 1 in thread
I have a app with QLineEdits. The app reads a file and put text in the
QLineEdits. What I want to do is when the user edit the text it must change
color. I have one slot that sets a flag that there was a change when ever the
user changed the text. I tried the following but it is not working.
QSignalMapper *signalMapper = new QSignalMapper(this);
QLineEdit *alfaEdit = new QLineEdit(this);
signalMapper->setMapping(alfaEdit, "alfaEdit");
connect(alfaEdit, SIGNAL(textEdited(const QString &)), signalMapper,
SLOT(map()));
connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SLOT(setModified(const QString &)));
setModified(const QString &text){
QPalette palette = signalMapper->mapping(text)->palette();
palette.setColor( palette.Text, Qt::red );
palette.setColor( palette.Base, Qt::blue );
signalMapper->mapping(text)->setPalette( palette );
if( !mModified){
mModified = true;
emit modified(mModified);
}
}
I know what the problem is, mapping return an Object and an Object doesn't
have a members 'palette' or 'setPalette'
Can someone pleas tell me how I can do this.
--
[ signature omitted ]
Message 2 in thread
Chris wrote:
> I have a app with QLineEdits. The app reads a file and put text in the
> QLineEdits. What I want to do is when the user edit the text it must change
> color. I have one slot that sets a flag that there was a change when ever the
> user changed the text. I tried the following but it is not working.
>
> QSignalMapper *signalMapper = new QSignalMapper(this);
> QLineEdit *alfaEdit = new QLineEdit(this);
> signalMapper->setMapping(alfaEdit, "alfaEdit");
> connect(alfaEdit, SIGNAL(textEdited(const QString &)), signalMapper,
> SLOT(map()));
> connect(signalMapper, SIGNAL(mapped(const QString &)),
> this, SLOT(setModified(const QString &)));
>
> setModified(const QString &text){
> QPalette palette = signalMapper->mapping(text)->palette();
> palette.setColor( palette.Text, Qt::red );
> palette.setColor( palette.Base, Qt::blue );
> signalMapper->mapping(text)->setPalette( palette );
> if( !mModified){
> mModified = true;
> emit modified(mModified);
> }
> }
>
> I know what the problem is, mapping return an Object and an Object doesn't
> have a members 'palette' or 'setPalette'
>
> Can someone pleas tell me how I can do this.
Hi Chris:
Well, you could simply use dynamic_cast to cast the object
returned by mapping() to a QWidget. This is rather ugly. I
would suggest either to subclass QLineEdit to extend it with
the functionality you want or, if this is not an option,
create a "EditController" QObject that you'll create as a
child of the QLineEdit; this "EditController" can then
implement the functionality you want.
Martin.
--
[ signature omitted ]
Message 3 in thread
On Wednesday 04 July 2007 17:00, Martin Irman wrote:
> Chris wrote:
> > I have a app with QLineEdits. The app reads a file and put text in the
> > QLineEdits. What I want to do is when the user edit the text it must
> > change color. I have one slot that sets a flag that there was a change
> > when ever the user changed the text. I tried the following but it is not
> > working.
> >
> > QSignalMapper *signalMapper = new QSignalMapper(this);
> > QLineEdit *alfaEdit = new QLineEdit(this);
> > signalMapper->setMapping(alfaEdit, "alfaEdit");
> > connect(alfaEdit, SIGNAL(textEdited(const QString &)), signalMapper,
> > SLOT(map()));
> > connect(signalMapper, SIGNAL(mapped(const QString &)),
> > this, SLOT(setModified(const QString &)));
> >
> > setModified(const QString &text){
> > QPalette palette = signalMapper->mapping(text)->palette();
> > palette.setColor( palette.Text, Qt::red );
> > palette.setColor( palette.Base, Qt::blue );
> > signalMapper->mapping(text)->setPalette( palette );
> > if( !mModified){
> > mModified = true;
> > emit modified(mModified);
> > }
> > }
> >
> > I know what the problem is, mapping return an Object and an Object
> > doesn't have a members 'palette' or 'setPalette'
> >
> > Can someone pleas tell me how I can do this.
>
> Hi Chris:
>
> Well, you could simply use dynamic_cast to cast the object
> returned by mapping() to a QWidget. This is rather ugly. I
> would suggest either to subclass QLineEdit to extend it with
> the functionality you want or, if this is not an option,
> create a "EditController" QObject that you'll create as a
> child of the QLineEdit; this "EditController" can then
> implement the functionality you want.
>
> Martin.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/
Thanks Martin for your reply. I'm new to qt and don't know what a
"EditController" is. However what I did to fix my problem was.
connect(alfaEdit, SIGNAL(textEdited(const QString &)),
signalMapper, SLOT(map()));
//use "setMapping( QObject * sender, QWidget * widget )"
signalMapper->setMapping(alfaEdit, alfaEdit);
// use signal "mapped ( QWidget * widget )"
connect(signalMapper, SIGNAL(mapped(QWidget *)),
this, SLOT(setModified( QWidget *)));
// and modified my slot to receive a QWidget *
void setModified( QWidget * widget ){
QPalette palette = widget->palette();
palette.setColor( palette.Text, Qt::red );
palette.setColor( palette.Base, Qt::cyan );
widget->setPalette( palette );
if( !mModified){
mModified = true;
emit modified(mModified);
}
}
--
[ signature omitted ]
Message 4 in thread
Chris wrote:
> Thanks Martin for your reply. I'm new to qt and don't know what a
> "EditController" is.
Hi Chris:
Sorry, I've been to brief in describing my suggestion. What I
meant was: create an object of a class derived from QObject
as a child of your QLineEdit. This class (because it's a QObject)
can have slots/signals and thus it could implement the
functionality you were looking for. This object would be a kind
of "edit controller". There is no "EditController" thing in Qt :)
I've found that this is a useful workaround for extending an
existing QObject derived class when it's not an option to
derive from that class.
Martin.
--
[ signature omitted ]
Message 5 in thread
chris,
as i understand your problem, signal mapper is not needed there
This is a small code snippet that shows how to do it, check and see whether
it helps you
if not please feel free to ignore.
static bool changed;
Test::Test( QWidget* parent, const char* name, WFlags fl )
: TestBase( parent, name, fl )
{
connect(settextPushButton,SIGNAL(clicked()),this,SLOT(setText()));
connect(colorChangeLineEdit,SIGNAL(textChanged(constQString&)),this,
SLOT(textChanged(const QString&)));
}
void Test::setText()
{
colorChangeLineEdit->unsetPalette();
colorChangeLineEdit->blockSignals(true);
colorChangeLineEdit->setText(QString("sample string"));
colorChangeLineEdit->blockSignals(false);
changed=false;
}
void Test::textChanged(const QString& text)
{
if(!changed)
{
QPalette pal;
QColorGroup cg;
cg.setColor( QColorGroup::Text, QColor( 85, 0, 255) );
pal.setActive( cg );
colorChangeLineEdit->setPalette( pal );
changed=true;
}
}
Jagath
Message 6 in thread
> chris,
>
> as i understand your problem, signal mapper is not needed there
> This is a small code snippet that shows how to do it, check and see whether
it helps you
> if not please feel free to ignore.
>
> static bool changed;
>
> Test::Test( QWidget* parent, const char* name, WFlags fl )
> : TestBase( parent, name, fl )
> {
> connect(settextPushButton,SIGNAL(clicked()),this,SLOT(setText()));
> connect(colorChangeLineEdit,SIGNAL(textChanged(constQString&)),this,
> SLOT(textChanged(const QString&)));
> }
>
> void Test::setText()
> {
> colorChangeLineEdit->unsetPalette();
> colorChangeLineEdit->blockSignals(true);
> colorChangeLineEdit->setText(QString("sample string"));
> colorChangeLineEdit->blockSignals(false);
> changed=false;
> }
> void Test::textChanged(const QString& text)
> {
> if(!changed)
> {
> QPalette pal;
> QColorGroup cg;
>
> cg.setColor( QColorGroup::Text, QColor( 85, 0, 255) );
> pal.setActive( cg );
> colorChangeLineEdit->setPalette( pal );
>
> changed=true;
> }
> }
>
>
> Jagath
Hi Jagath
Thank you for the example. The reason that I want to use QSignalMapper is that
I have a lot of QLineEdits and I have to know which one emits the
textEdited() signal. I only want to change the QLineEdit that was edited.
Chris
--
[ signature omitted ]