cellClicked and cellPressed not working for QTableWidget - qt

Whats is wrong with following call. IDBTable is QTableWidget
connect(m_GUI->m_UI->IDBTable,SIGNAL(cellPressed(2, 0)), this, SLOT(slotGoToWelcomeScreen()));

Your signal definition is wrong. There is no such signal cellPressed(2, 0) but only cellPressed(int, int). Please read the documentation about signals and slots and prefer the new signal slot syntax to catch such errors during compile time.

Related

How to keep the source signal's parameters while using QSignalMapper?

I ran into a problem that I need to keep the mapped source signal's parameters. So far I only found examples to map signals without any parameter.
For example, the clicked() signal:
signalMapper = new QSignalMapper(this);
signalMapper->setMapping(taxFileButton, QString("taxfile.txt"));
connect(taxFileButton, SIGNAL(clicked()),
signalMapper, SLOT (map()));
connect(signalMapper, SIGNAL(mapped(QString)),
this, SLOT(readFile(QString)));
However, I would need to map some signal with its own parameters, for example the clicked(bool) signal, then the SLOT need to have two arguments doStuff(bool,QString):
connect(taxFileButton, SIGNAL(clicked(bool)),
signalMapper, SLOT (map()));
connect(signalMapper, SIGNAL(mapped(QString)),
this, SLOT(doStuff(bool,QString)));
However, it does not work like this? Is there any work around?
Thanks!
QSignalMapper does not provide functionality to pass signal parameters.
see documentation:
This class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal.
There are the ways to solve that:
If Qt4 is used then I'd suggest to implement your own signal mapper which supports parameters what you need.
QSignalMapper implementation would be good example to start.
But if Qt5 is used then you can do exactly what you need without usage QSignalMapper at all. Just connect signal to lambda:
connect(taxFileButton, &TaxFileButton::clicked, [this](bool arg) {
doStuff(arg, "taxfile.txt");
} );
I assume taxFileButton is instance of TaxFileButton class.
If C++11 lambda is not suitable for some reason then tr1::bind could be used to bind this and "taxfile.txt" values.
Please note such connection will not be disconnected automatically when this object is destroyed.
More details are here.

QTableWidget cellClicked signal not working

I have a QTableWidget in my application and I have connected the cellClicked(int,int) signal to a slot. But this code in the slot doesn't get called at all when a cell is clicked. Please let me know how this can be resolved. This is my code:
connect(ui.tableWidget, SIGNAL(cellClicked(x,y)), this, SLOT(myCellClicked(x,y)));
Thanks,
Rakesh.
The SIGNAL and SLOT macros only handle type names, not variable names, so it should be:
connect(ui.tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(myCellClicked(int,int)));

Qt connect slot with signal from boost::shared_ptr

I have a mainwindow app, when shortcut is triggered, a dialog will popup to show some information, the user may do some configuration in this dialog, then a signal is sent back to the mainwindow, the mainwindow will do some further work. the pseudo code looks like this:
void MainWindow::actionConfigure_triggered()
{
configureDialog = boost::shared_ptr<configure>(new configure(this));
configureDialog->show();
connect(configureDialog.get(), SIGNAL(reload()), this, SLOT(clean_reload()));
}
but when I triggered this function several times, segmentation fault happens. I use debugger to trace the execution, SIGSEGV received when executing boost::checked_delete function.
Any help will be highly appreciated! Thanks in advance.
I just want the configure dialog to be created and deleted dynamically, or there are other better ways to implement this?
According to your backtrace the bug seems somewhere in destructor of configure and has little to do with the shared_ptr (except that it is the shared_ptr that calls the destructor)
Check if there are double deletes of your object, if yes there is probably some other reference to it which is not a shared_ptr deleteing the object.

Qt SLOTS parameters (Beginner)

I'm a beginner to Qt and am making (or at least trying to make) a basic calculator. If I understand correctly, when doing this:
connect(my_button_4, SIGNAL(clicked()), this, SLOT(writeNumberLbl("4")));
The "4" is not accessible (rather, only its type is) in writeNumberLbl. Basically, I would like so that when the button is clicked, the label sets its text to "4". However, I have the numbers 0 to 9, so I wanted to do:
connect(my_button_0, SIGNAL(clicked()), this, SLOT(writeNumberLbl("0")));
connect(my_button_1, SIGNAL(clicked()), this, SLOT(writeNumberLbl("1")));
...
connect(my_button_9, SIGNAL(clicked()), this, SLOT(writeNumberLbl("9")));
My writeNumberLbl function is:
void Calculator::preWriteVal(QChar val)
{
QString curVal = ui.lbl_output->text();
curVal += val;
ui.lbl_output->setText(curVal);
}
However, I can see that this will not work due to the parameter, 'val'. Could someone please point me in the right direction? Thank you. I did look to see if this question had already been answered and couldn't find anything. If it has, please provide me a link.
Also, is it possible, using Qt Designer 4, to connect a widget to a custom slot?
Thank you.
As far as I know,Qt's signal/slot system requires that the number of parameters of signal function is not less than that of slots function.
In your example,the signal function click() have zero parameters and slot function writeNumberLbl("4") have one parameter,this will not be accepted by the Qt's signal/slot system and if you run your program in debug mode of Qt creator,the qDebug will show you a message like 'Incompatiable signal/slot' blalbalba~. To solve this problem, just read the article given by Arnold Spence. It is quite clear.
There are a number of ways to tackle this problem and they are outlined very nicely here. Although that page is a bit old, I think it is still quite valid. I would recommend using a signal mapper.
For your second question, yes. You can connect signals and slots using Qt Designer by setting the designer in "Edit Signals/Slots" mode. Once in this mode, for example, you can drag a connection line from a button to the form. A dialog will open up allowing you to choose the signal and slot to connect. If you haven't already implemented a slot in code, you can specify the name of a slot and then add the code for it afterward.
The number of parameters in Slot can not exit those in Signal? and pressed() has none. You have two choices (three, counting the dumb one):
Use QSignalMapper. Its help is self-explanatory.
Connect all your buttons to single slot. In it, find out what button has been pressed. QObject::sender() function helps.
There are even more ways, but more complicated.

Can anyone give me same someting to keep in mind while using signals and slots in Qt?

I am learning to program using Qt framework. When I writes some code have signals and slots involved, the events didn't seem to fire and the signals and slots didn't seem to work. It really me make me annoyed. Can you give me some cautions and warnnings about signals and slots in Qt?
slot declarations:
private slots:
void onFtpCmdFinish(int cmdId, bool error);
void onRealtimeFtpCmdsDone(bool error);
connection code:
ftpHandle = new QFtp( this );
connect(ftpHandle, SIGNAL(commandFinished(int, bool)), this, SLOT(onFtpCmdFinish(int, bool)));
connect(ftpHandle, SIGNAL(done(bool)), this, SLOT(onRealtimeFtpCmdsDone(bool)));
Thank you in advance!
In the future, if you ever happen to run into problems with your Qt signals and slots again, the contents of the following blog entry can turn out to be a real life-saver (or at least a very good starting point for your investigations):
http://samdutton.wordpress.com/2008/10/03/debugging-signals-and-slots-in-qt/
It meticulously lists 20 ways to debug/troubleshoot your signals and slots; follow this list and chances are high that you will eventually find out what's wrong.
I hope that helps.
You can only detect failed connect() at runtime.
A couple of tips:
defining QT-FATAL-WARNINGS=1 will cause Qt to assert and quit whenever it gets a connect that doesn't match.
Or wrapping each connect in:
bool ok = connect(……); QASSERT( ok);
Always check the return type, if its true then CONNECT successful else some thing wrong..
Don't forget about the fifth argument Qt::ConnectionType if you will write multithreaded applications

Resources