You are not logged in.

ashken

Beginner

  • "ashken" is male
  • "ashken" started this thread

Posts: 12

Location: somewhere

Occupation: misc

  • Send private message

1

Monday, June 14th 2010, 10:09pm

Craeting custom SLOT

Am using Qt4 on linux to create a text editor.so i wanted to implement the About menu with a custom slot but when i launch the application from the terminal i get the message that the slot about() does not exist.Here is my code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
****menu.h*********
#ifndef MENU_H
#define MENU_H
#include <QMainWindow>

class menu:public QMainWindow
{
 
   public:
   menu(QWidget *parent = 0);
   
   public slot:
   void about();
    
};
#endif


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "menu.h"
#include <QMenu>
#include <QMenuBar>
#include <QApplication>
#include <QToolBar>
#include <QStatusBar>
#include <QTextEdit>
#include <QIcon>
#include <QMessageBox>


menu::menu(QWidget *parent):QMainWindow(parent)
{
   QPixmap newp("icon1.xpm");
   QPixmap openp("open.xpm");
   QPixmap savep("save.xpm");
   QPixmap printp("print.xpm");
   QPixmap undop("undo.xpm");
   QPixmap redop("redo.xpm");
   QPixmap cutp("cut.xpm");
   QPixmap copyp("copy.xpm");
   QPixmap pastep("paste.xpm");
   QPixmap fontp("font.xpm");

   QAction *newa = new QAction("&New",this);
   QAction *open = new QAction("&Open",this);
   QAction *save = new QAction("&Save", this);
   QAction *saveAs = new QAction("&SaveAs",this);
   QAction *print = new QAction("&Print",this);
   QAction *quit = new QAction("&Quit",this);

   QAction *undo = new QAction("&Undo",this);
   QAction *redo = new QAction("&Redo",this);
   QAction *cut = new QAction("&Cut",this);
   QAction *copy = new QAction("&Copy",this);
   QAction *paste = new QAction("&Paste",this);
   QAction *font = new QAction("&Font",this);
   QAction *selectAll = new QAction("&Select All",this);
   
   QAction *toolb = new QAction("&Toolbar",this);
   QAction *statusb = new QAction("&Statusbar",this);
   
   QAction *helb = new QAction("&About",this);
   
   QMenu *file;
   file=menuBar()->addMenu("&File");
   file->addAction(newa);
   file->addAction(open);
   file->addSeparator();
   file->addAction(save);
   file->addAction(saveAs);
   file->addSeparator();
   file->addAction(print);
   file->addSeparator();
   file->addAction(quit);
   
   QMenu *editm;
   editm=menuBar()->addMenu("&Edit");
   editm->addAction(undo);
   editm->addAction(redo);
   editm->addSeparator();
   editm->addAction(cut);
   editm->addAction(copy);
   editm->addAction(paste);
   editm->addSeparator();
   editm->addAction(font);
   editm->addSeparator();
   editm->addAction(selectAll);
   
   QMenu *view;
   view=menuBar()->addMenu("&View");
   view->addAction(toolb);
   view->addAction(statusb);
   
   QMenu *help;
   help=menuBar()->addMenu("&Help");
   help->addAction(helb);
   
   QToolBar *toolbar = addToolBar("new toolbar"); 
   toolbar->addAction(QIcon(newp),"New file");
   toolbar->addAction(QIcon(openp),"Open");
   toolbar->addAction(QIcon(savep),"Save");
   toolbar->addSeparator();
   toolbar->addAction(QIcon(printp),"Print");
   toolbar->addSeparator();
   toolbar->addAction(QIcon(undop),"Undo");
   toolbar->addAction(QIcon(redop),"Redo");
   toolbar->addSeparator();
   toolbar->addAction(QIcon(cutp),"Cut");
   toolbar->addAction(QIcon(copyp),"Copy");
   toolbar->addAction(QIcon(pastep),"Paste");
   toolbar->addSeparator();
   toolbar->addAction(QIcon(fontp),"Font");

   QTextEdit *edit = new QTextEdit(this);
  
   setCentralWidget(edit);
   
   //QStatusBar *statusbar = new QStatusBar(this);
   statusBar()-> showMessage("ready");
   connect(quit,SIGNAL(triggered()),qApp,SLOT(quit()));
   connect(helb,SIGNAL(triggered()),this,SLOT(about()));
}
 
void menu::about()
{

  QMessageBox::about(this,"About ashEdit",
                        "<h2>  ashEdit </h2>"
                        "<p>Right to copy &copy;2010 Dev. Inc."
                        "<p> ashEdit is a small Text Editor Application "
                        "by vinnie ashken "
                        "Comments and suggestions can be emailed to:   "
                        "vinnieashken@eml.cc ");
}

int main(int argc,char *argv[])
{
  QApplication app(argc,argv);
  menu window;
  window.setWindowTitle("Ashken");
  window.resize(500,450);
  window.show();
   
  return app.exec();
}

Help me out pls, lest i decide to go back to wxwidgets and win32 APIs.

2

Tuesday, June 15th 2010, 5:16am

Where is Q_OBJECT macro?
If you need to develop only for win32 platform consider .NET.
Fighting fire with fire.
Three can keep a secret if two of them are dead.

Znurre

Trainee

  • "Znurre" is male

Posts: 173

Location: Sweden

Occupation: Programmer

  • Send private message

3

Tuesday, June 15th 2010, 6:14am

As Messenger pointed out, you need the Q_OBJECT macro in your header.

Source code

1
2
3
class menu:public QMainWindow
{
 	Q_OBJECT

It's also not slot but slots.

Source code

1
public slots:

Concerning .NET, I don't want to start a flamewar about it but I use it daily in my work and I would choose Qt4/C++ over C#/.NET anytime no matter what platform I create applications for.
The Qt4 API is much nicer, the layouts in Windows.Forms in .NET sucks, and having a garbage collector sucks even more though the C# one is better than Java's atleast.

ashken

Beginner

  • "ashken" is male
  • "ashken" started this thread

Posts: 12

Location: somewhere

Occupation: misc

  • Send private message

4

Tuesday, June 15th 2010, 9:18am

Thanks guys for the tips but when i add the Q_OBJECT macro i get this errors, i don't know why it is happening to me.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
root@vini-desktop:~/Desktop/spread# make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -o app.o app.cpp
g++ -Wl,-O1 -o spread app.o    -L/usr/lib -lQtGui -lQtCore -lpthread
app.o: In function `menu::menu(QWidget*)':
app.cpp:(.text+0xee): undefined reference to `vtable for menu'
app.cpp:(.text+0xf5): undefined reference to `vtable for menu'
app.o: In function `main':
app.cpp:(.text+0x1bf5): undefined reference to `vtable for menu'
app.cpp:(.text+0x1bfd): undefined reference to `vtable for menu'
app.cpp:(.text+0x1c46): undefined reference to `vtable for menu'
app.o:app.cpp:(.text+0x1c4e): more undefined references to `vtable for menu' follow
collect2: ld returned 1 exit status
make: *** [spread] Error 1


as for an alternative to C++, never! as far as desktop apps are concerned, Znurre is right, C++ is far better than any other language,i don't like interpreted languages and as for windows, the Win32 APIs are far better than any alternative.

Znurre

Trainee

  • "Znurre" is male

Posts: 173

Location: Sweden

Occupation: Programmer

  • Send private message

5

Tuesday, June 15th 2010, 9:22am

Run qmake again, and then try to build again.
This will solve your problem.

ashken

Beginner

  • "ashken" is male
  • "ashken" started this thread

Posts: 12

Location: somewhere

Occupation: misc

  • Send private message

6

Tuesday, June 15th 2010, 9:28am

@Znurre IT WORKED! thanks all for the help.