[Solved] How to see custom slot in signal slot editor
I'm using Qt Creator 2.0.1 and I have a custom slot my QMainWindow ... now I have a pushbutton, which on clicked should call the custom slot on the main window. Can do in code yes, but can't do this with the signal-slot editor.
When I open the signal-slot editor, I see the custom slot on the right but the entire set of slots are disabled. When I select the clicked() signal of the pushbutton, all the slots disappear on the right and I cannot get them back. Basically this is not supported.
Anyone got this working?
The disappearing of the slots on the right is an indication that the signal/slot signatures do not match. Only matching slots are displayed.
[quote author="Friedemann.Kleint" date="1283946542"]The disappearing of the slots on the right is an indication that the signal/slot signatures do not match. Only matching slots are displayed.[/quote]
Nope, I ensured that the signal/slot signatures were matching, still doesn't work.
Can you give a sample code snip?
Not exactly code since we want to do this in Designer, but to make sure we are not talking at cross purposes, what I did is:
- Start a QMainWindow form
- Context menu 'Change Signals/Slots'
- added test(bool)
- Drop a QPushButton on the form, switch to Signal/Slot mode
- Drew a connection from button to form and connected clicked(bool) with test(bool).
One thing that might cause problems is the 'canonical' signal/slot signature, that is, Designer permits only 'SLOT ( textChanged ( QString ) )' and not a parameter specified as a const-reference.
yes your steps got it working, thanks for the same :)
After doing your steps, then I've to go and add the relevant code in the mainwindow .h and .cpp files ...
But seems like lot of inconsistencies .. and hard to figure out !
- No context menu for pushbutton to edit signal/slots
- Any custom signal/slot added in code is not available to the designer - it appears and disappears as mentioned earlier :)
- Why not a parameter which is a const?
- Not documented anywhere is my guess ... pls correct me on that
No context menu for pushbutton to edit signal/slots
You can specify custom signals and slots only for promoted widgets or main forms. Otherwise, slots and signals are determined by introspection.
Why not a parameter which is a const?
Not documented anywhere is my guess … pls correct me on that
You can use a const-references, freely, but the 'canonical' slot signature will still be " SLOT ( textChanged ( QString ) )". Admittedly, the documentation is not easy to find, for which there is a bug report pending ( "QTBUG-8961":http://bugreports.qt.nokia.com/browse/QTBUG-8961 )
[ edited to make the bug clickable, tobias ]
Friedemann: thanks for the details :)
The latest solution!
I've started working with Qt 2 days ago and I'm already sniffing the Qt Creator IDE. Actually, there IS a way to create and use custom slots on the "Signals & Slots Editor". The tricky is:
- Right click your QMainWindow widget on the design view OR right click the QMainWindow on the Object Inspector;
- Choose "Change Signals/Slots..." on the menu.
Shall I explain the given screen or you guys can figure it out? ;)
thanks Cabuk ... should've seen this :)
one more question, once this custom slot is added in the designer, how and where do I code for it?
It gets added in Ui_MainWindow.h, editing that directly is not recommended.
Why you think that it's not "recommended"? My concept is that the Qt Creator - and any other IDE - is here to help us code, making it easy, simple, and fast to do. The Qt Creator is no exception.
About the method I explained, it's meant to be hand coded by declaring the existence of custom signals/slots to the IDE.
Alternatively you can use the "automatic" slot generation, which creates "ugly method names" - in my opinion -, but works too:
- Right click any widget or any action on the Action Editor
- choose the option Go to slot...
- Choose your signal and click OK.
This way the Qt Creator will generate the slot definition and declaration (.h and .cpp files), and will show you the newly created slot on the cpp.
PS.: You can create these "automatic" slots by hand, since the Qt compiler will detect it, but this isn't recommended, for real :P
@cubuk, in my opinion, I meant, hand tweaking the generated ui header file is not recommended, as next time you make UI changes in designer, it will get overridden ...
... ok using designer this is what I want to do.
In my mainwindow, I want a custom slot "void mySlot()" and I want to add code into it. I want to click a button and invoke mySlot on mainwindow.
Now can you help in step by step instructions, how to go about it using designer to create the custom slot...
No, you won't hand code the ui file. Do not confuse the C++ class files (.h and .cpp) and the user interface file (.ui). This second one is modified on the designer view, where my tip comes. The class files are the code you'll write. If you indicate on the designer your custom signals/slots - using my method -, it won't reflect on the class files. It just says that your class have - or will have - the given signal/slot.
- Add the slot on the designer view to your QMainWindow
- Create the definition of your slot on the .h file:
- Create the declaration of your slot on the .cpp file:
// Add your code here
- Run it ;)
This is the expected behavior since the .h and .cpp files are meant to be hand coded by you. Did I answer your question? :D
so you create the custom slot in designer and then write code for it in mainwindow.h and mainwindow.cpp. yes that works.
now, suppose I already have a custom slot(s) coded in my mainwindow.h, why doesn't it show up in the signal/slot editor for me to connect them? that was my original question. Suppose I already have coded many slots, connecting them via signal slot editor would be a good feature I think. Or is there a way to get these too and I missed it yet again :P
bq. If you indicate on the designer your custom signals/slots – using my method -, it won’t reflect on the class files.
That's the point you missed... By using the method I said, it won't touch your class files, so you can declare here all your already-made slots with ease, and use it on the Signal & Slots Editor :D
ok ok i get it and thanks for the replies :)
You're welcome! And sorry if I misunderstood your question, but I just wanted to be the most verbose possible to explain it :o
i learnt a new trick with the creator :)