// Customwnd. h
# Ifndef _ custom_window_h __
# DEFINE _ custom_window_h __
# Include <qapplication>
# Include <qwidget>
# Include <qmessagebox>
# Include <qmenu>
Class customwnd: Public qwidget
{
Q_object // If You Want to customize the slot and message, you must call this macro here. Otherwise, the custom slot and message will not work.
Public:
Customwnd (qwidget * parent = 0, const char * name = NULL );
~ Customwnd ();
Public slots: // custom slots. If a custom slot is required, it is declared as protected slots:
Void btnmessage (); // customize the slot without parameters.
Void slottest (qstring); // custom slot.
Signals: // custom signal. If you want to customize the protection signal, it will be declared after protected.
// You only need to declare the custom signal here, and then connect the slot to the signal, without implementing the signal function. the return value types of the signal function and the slot function can be different at any time. If you do not care about the parameters passed by the signal, the parameter lists of the signal function and the slot function can also be different, however, if you want to access any parameters transmitted by the signal, the list of parameters of the signal function and the slot function must be the same.
Void explains (); // If You Want to customize the slot and signal, the explains signal is required.
Void sigTest (QString str); // custom signal.
Private:
QPushButton * m_pushBtnMsg;
};
# Endif
// Customwnd. cpp
CustomWnd: CustomWnd (QWidget * parent = 0, const char * name = NULL)
: QWidget (parent, name)
{
M_pushBtnMsg = new QPushButton ("MessageButton", this );
M_pushBtnMsg-> show ();
Connect (m_pushbtnmsg, signal (clicked (), this, slot (btnmessage ()));
// Connect the custom slot to the internal signal, which is similar to the Message ing function.
Connect (this, signal (sigtest (qstring), this, slot (slottest (qstring )));
// Connect the custom slot to the custom Signal
}
Customwnd ::~ Customwnd ()
{
Delete m_pushbtnmsg;
}
Void customwnd: btnmessage ()
{
Qmessagebox: Warning (this, "warning", "just for test: will emit test signal ");
Emit sigtest (qstring ("test signal"); // sends a custom Signal
}
Void customwnd: slottest (qstring Str)
{
Qmessagebox: Warning (this, "customized signal test", STR );
}
// Test. cpp
# Include "customwnd. H"
Int main (INT argc, char ** argv)
{
Qapplication A (argc, argv );
Customwnd WND;
A. setmainwidget (& WND );
WND. Show ();
Return a.exe C ();
}
We can use qmake-project; qmake to enable QT tool to automatically generate makefile. However, people who are used to writing makefile can see a lot of code in the automatically generated makefile, A makefile template is provided below.
// Makefile
# Qtver = 3.3.2
Qtver = 4.3.1
Ifeq ($ (qtver), 3.3.2)
Qtpath =/usr/lib/qt-3.3.2
Cflags =-I $ (qtpath)/include
Ldfalgs =-L $ (qtpath)/lib
Libs =-lqt-MT
Endif
Ifeq ($ (qtver), 4.3.1)
Qtpath =/usr/local/trolltech/Qt-4.3.1
Cflags =-I $ (qtpath)/include/Qt-dqt3_support
Ldfalgs =-L $(qtpath)/lib-L/usr/x11r6
Libs =-lqtgui
Endif
# MOC is a QT Tool
MOC = $ (QTPATH)/bin/moc
# Be sure to note that if there are several versions of QT at the same time, the correct moc tool must be used; otherwise, an inexplicable error may occur during compilation of the moc document.
MOCOBJSUFFIX = moc. o
MOCSRCSUFFIX = moc. cpp
MOCOBJS =$ (MOCHEADERS: %. h = %. $ (MOCOBJSUFFIX ))
BIN = test
OBJS = test. o customwindow. o
# If the class declaration contains Q_OBJECT, add the corresponding header document to MOCHEADERS.
MOCHEADERS = custonwnd. h
All: $ (BIN)
$ (BIN): $ (OBJS) $ (MOCOBJS)
$ (CXX) $ (LDFLAGS)-o $ (LIBS)
%. O: %. cpp
$ (CXX) $ (CFLAGS) $ (CXXFLAGS)-o $ @-c $ <
# Define implicit rules for. o documents generated by moc documents
%. $ (MOCOBJSUFFIX): %. $ (MOCSRCSUFFIX)
$ (CXX) $ (CFLAGS) $ (CXXFLAGS)-o $ @-c $ <
# Define implicit rules for generating moc documents by *. h
%. $ (MOCSRCSUFFIX): %. h
$ (MOC)-o $ <
Clean:
$ (RM) $ (BIN) *. MoC *. o