Creating a custon slot for a QMenu
-
25-06-2021 - |
Question
I'm new in Qt World)
I created a new Qt Application in MSVC 2008
Using Qt Creator added controls I need and one of them is QMenuBar
As I understend the equivalent of CallBack(C#) is Slot in Qt.
I culdn't find any information how to create a custom Slot for QMenu using Qt Creator.
Solution
- Subclass QMenuBar, and call the new class whatever you want, e.g.,
FancyMenuBar
. - Add the
Q_OBJECT
macro in your class definition (google for more info) in fancymenubar.h. - Add the line
public slots:
in your class definition, e.g., somewhere betweenpublic:
andprivate:
. - Add the slot definition under that line, e.g.,
void fancySlot();
. - Implement the slot definition, e.g.,
(in fancymenubar.cpp)
void FancyMenuBar::fancySlot()
{
// type code here
}
Now you can use the slot via the QObject::connect()
function, or use the slot as if it were a normal public function.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow