confused by clicked() and clicked(bool) in qt 4 signal and slot - qt

I am using qt 4.8.6 and visual studio 2008 to develop a project and confused by clicked() and clicked(bool). While building a connection for an object which will emit a signal:
connect(sender, SIGNAL(clicked(bool)), receiver, SLOT(myslot()));
will trigger myslot(); and
connect(sender, SIGNAL(clicked()), receiver, SLOT(myslot()));
will not trigger it. However, I find other many examples about connect which all use clicked() not clicked(bool). Why cannot I use clicked()?
I look through Qt Assistant about:
void QAbstractButton::clicked ( bool checked = false ) [signal]
This signal is emitted when the button is activated (i.e. pressed down then released while the mouse cursor is inside the button), when the shortcut key is typed, or when click() or animateClick() is called. Notably, this signal is not emitted if you call setDown(), setChecked() or toggle().
If the button is checkable, checked is true if the button is checked,
or false if the button is unchecked.
I cannot find its reason. At the same time, what are the differences of the "checked" and "unchecked"?
By the way, I build a connect by pressing the left mouse button and drag the cursor. Another way is to rightclick the object, then the context menu will applear "go to slot", but my Qt Designer(4.8.6) will not. How to deal with it?
3 quesions hope to get help. Many thanks in advance.

I'm not sure I really understand the question(s), but the reason you can't connect to a clicked() signal is because there is no such thing... the function profile is clearly clicked(bool) (see docs).
Qt will only show a runtime error when it can't connect signal/slot (qWarning to stderr), it is not obvious at compile-time. Try examining the program output for warnings.
Edit: removed misleading information.

I encountered the same phenomenon too.
I think clicked(bool) and clicked(bool = false) is different.
The signal has different events.
I guess your "myslot()" may trigger the second event.

Related

emit signal if cell of QTableView is clicked [duplicate]

I have a QTableView in a PyQt application, and I want to keep track of when the selection changes. I've tried connecting the signal to a slot as follows (using the advice on this page:
self.view.selectionModel().selectionChanged.connect(self.selChanged)
where the slot it is connected to is defined as:
def selChanged(self, selected, deselected):
print "Sel changed"
However, whenever I load the QMainWindow on which the QTableView resides, I get an immediate segmentation fault.
Am I doing something silly here?
I was having a similar problem and the fix was here:
PySide: Segfault(?) when using QItemSelectionModel with QListView
Namely, replace:
self.view.selectionModel().selectionChanged.connect(self.selChanged)
with two commands:
selectionModel = self.view.selectionModel()
selectionModel.selectionChanged.connect(self.selChanged)
Not sure why this works, frankly.
This has been fixed now, it turned out that I was using an old version of Qt on that machine - which seemed to cause the crash.
The moral of the story is: if your code is crashing for no sensible reason, check all of your dependencies (in this case Qt and PyQt) are up-to-date.

Function connected to the button is called twice after one click

I have a problem with slots and signals. I created buttons and connected them to the clicked() slot. Then i decided to connect signals and slots manually and since then when I click the button it calls its function twice.
connect(ui->okButton, SIGNAL(clicked()), this, SLOT(on_okButton_clicked()));
void settingswindow::on_okButton_clicked()
{
qDebug() << "ok clicked";
this->close();
}
I was looking for the answer on google, but all i found was this: Where is the generated code of qt signals slots editor but my *.ui file looks like this: pastebin to the code. As you can see there's only one line with and nothing more. I can't find where the information about signals and slots is saved. Rebuild and clean options won't help.
This is not a bug in Qt. If you look at the generated code for your ui_*.h file, you'll notice that the last statement executed in the setupUi() function is a call to QMetaObject::connectSlotsByName().
Since your slot already conforms to the naming convention that this function is looking for, your slot is automatically connected to the signal.
By manually connecting the signal to the slot, in your settingswindow class, you effectively duplicate the connection.
As #Devopia mentioned, this is a documented feature.

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.

QT signals & slots

How to use signal and slots for handling events i.e when a pushButton is clicked some number should be displayed in QLineEdit.Just like in caluculator application when a number is clicked the corresponding is displayed .
connect a widget's signal to other's widget slot.
Signal = Emitted when the button (for instance) is pressed.
Slot = called when a certain singal is fired, and this signal is connected to this slot.
If You use QtCreator here is the procedure:
right click on the pushbutton. Select go to slot.
Select clicked()
QtCreator will open C++ editor with new empty member function defined that implements
your slot. This function will be called whenever someone clicks this button.
Now in this function you can call method of another widget ot modify its contants.
This book is quite good for starters:
http://www.amazon.com/Book-Qt-Art-Building-Applications/dp/1593271476/ref=sr_1_8?ie=UTF8&qid=1300905855&sr=8-8
Good luck.

Resources