Seite 1 von 1

signal und slot (wahrscheinlich zum x ten mal

Verfasst: 5. Februar 2007 13:09
von waldo
hallo leute,
ich glaub zwar das einige genervt sind von der Frage die ich hier stelle aber ich versuch es doch einmal ;)
Ich versuche unter Windows mit dem Microsoft VC 2005 Studio eine Aplikation zu erstellen. Ich hoffe das ich die Qt4 Bibliotheken richtig eingebunden zu haben, jedenfalls läßt es sich compelieren und starten. Mein Problem ist jetzt das ich ein Menü erstellen möchte (oder habe) und wenn der Anwender auf ein Menüunterpunkt clicked möcht ich das eine Procedur aufgerufen wird. Das Problem ist nur das ich in der Console immer den Hinweiß bekomme das die slots nicht bekannt sind.
Vielleicht kann mir ja jemand einen Tip geben wo der Fehler ist.
Mein main Teil:
#include "stdafx.h"
#include <QApplication>
#include <QPushButton>
#include <QTreeView>
#include <QListWidget>
#include <QHeaderView>
#include <QStandardItemModel>
#include <QtGui>


#include "RandCPmp3.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
int width = 800, hight = 600;

CRandCPmp3 RandCPmp3 (NULL, "RandCPmp3", width, hight);
RandCPmp3.Go ();
RandCPmp3.show();


return app.exec();
}
Mein *.h Teil:
#ifndef RANDCPMP3_H
#define RANDCPMP3_H

#define _FILE_MENU "File"
#define _OPTION_MENU "Option"

#include <QApplication>
#include <QWidget>
#include <QTreeView>

class CRandCPmp3 : public QWidget
{
// Q_OBJECT

public:

CRandCPmp3();
CRandCPmp3(QWidget *parent = 0, const char *name = 0, int width = 800, int hight=600);
/* ~CRandCPmp3()
{}
*/
void Go ();

private:
QMenu *test;
QAction *testAct;
protected:
void Resize ();


char* itoa (int a, char *str, int base);
char* itoa (long a, char *str, int base);
char* itoa (unsigned long a, char *str, int base);

public slots:
void slotEnd ();
void slotGetExistDirMp3 ();
void slotGetCreateDirMp3 ();
void slotSelectRandomMp3 ();
void slotTest (int);

signals:
void call ();
};

#endif // RANDCPMP3_H

Und mein *.cpp Teil:
#include "stdafx.h"
#include "RandCPmp3.h"

#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include <QApplication>
#include <QWidget>
#include <QTreeView>
#include <QListWidget>
#include <QHeaderView>
#include <QStandardItemModel>
#include <QtGui>

/*
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
*/


CRandCPmp3::CRandCPmp3(QWidget *parent, const char *name, int width, int hight)
: QWidget(parent)
{

QMenuBar *menuBar;
menuBar = new QMenuBar (this);
QMenu *file, *option;

file = new QMenu (_FILE_MENU, this);
option = new QMenu (_OPTION_MENU, this);
test = new QMenu ("test", this);

menuBar->addMenu (file);
menuBar->addMenu (option);
menuBar->addMenu (test);


testAct = new QAction("&Get directory with contained mp3 files", (QObject*) menuBar);
testAct->setShortcut(tr("Ctrl+G"));
testAct->setStatusTip(tr("Open an existing file"));
connect(testAct, SIGNAL(triggered()), this, SLOT(slotTest(1)));
test->addAction(testAct);
test->addSeparator ();


//add item for first menu
QAction *openAct;
openAct = new QAction("&Get directory with contained mp3 files", (QObject*) this);
openAct->setShortcut(tr("Ctrl+G"));
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(slotGetExistDirMp3()));
file->addAction(openAct);
file->addSeparator ();

//add item for first menu
openAct = new QAction("S&elect (or create) directory to save the selected mp3 files", (QObject*) this);
openAct->setShortcut(tr("Ctrl+S"));
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(slotGetCreateDirMp3()));
file->addAction(openAct);
file->addSeparator ();

//add item for first menu
openAct = new QAction(tr("&Exit"), (QObject*) this);
openAct->setShortcut(tr("Ctrl+E"));
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(slotEnd()));
file->addAction(openAct);
file->addSeparator ();

//add item for second menu
openAct = new QAction("Select &random MP3(s)", (QObject*) this);
openAct->setShortcut(tr("Ctrl+R"));
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(slotSelectRandomMp3()));
option->addAction(openAct);
option->addSeparator ();


QTreeView *sourceView;
sourceView = new QTreeView (this);
sourceView->setRootIsDecorated(false);
sourceView->setAlternatingRowColors(true);

QStringList longerList = (QStringList() << "Number" << "File" << "Space");
QStandardItemModel *model = new QStandardItemModel (3,2,this);
model->setHorizontalHeaderLabels (longerList);
sourceView->setModel(model);
sourceView->show();
sourceView->move ( 200, 300 );


resize(width, hight);


}
CRandCPmp3::CRandCPmp3()
/* : QWidget(parent)*/
{

}


char* CRandCPmp3::itoa (int a, char *str, int base)
{
// strcpy (str, "%a");
char tmp [15] = "";
//clear the string
memset (tmp, 0, sizeof(tmp));
switch (base)
{ case 16:
sprintf (tmp, "%x", a);
break;
case 8:
sprintf (tmp, "%o", a);
break;
default :
sprintf (tmp, "%i", a);
break;
}
strcpy (str, tmp);
return str;
}
char* CRandCPmp3::itoa (long a, char *str, int base)
{
// strcpy (str, "%a");
char tmp [25] = "";
//clear the string
memset (tmp, 0, sizeof(tmp));
switch (base)
{ case 16:
sprintf (tmp, "%x", a);
break;
case 8:
sprintf (tmp, "%o", a);
break;
default :
sprintf (tmp, "%d", a);
break;
}
strcpy (str, tmp);
return str;
}
char* CRandCPmp3::itoa (unsigned long a, char *str, int base)
{
// strcpy (str, "%a");
char tmp [25] = "";
//clear the string
memset (tmp, 0, sizeof(tmp));
switch (base)
{ case 16:
sprintf (tmp, "%x", a);
break;
case 8:
sprintf (tmp, "%o", a);
break;
default :
sprintf (tmp, "%d", a);
break;
}
strcpy (str, tmp);
return str;
}

void CRandCPmp3::Go ()
{int a;

a = 1;


}

void CRandCPmp3::Resize ()
{
}

void CRandCPmp3::slotEnd ()
{
qDebug("slotEnd () is called");
}

void CRandCPmp3::slotGetExistDirMp3 ()
{
qDebug("slotGetExistDirMp3 () is called");
}

void CRandCPmp3::slotGetCreateDirMp3 ()
{
qDebug("slotGetCreateDirMp3 () is called");
}

void CRandCPmp3::slotSelectRandomMp3 ()
{
qDebug("slotEnd () is called");
}

void CRandCPmp3::slotTest (int a)
{
qDebug("slotTest () is called");
}

void CRandCPmp3::call ()
{
qDebug("call () is called");
}


Über Hinweise wäre ich sehr dankbar.

gruß

Verfasst: 5. Februar 2007 13:56
von Christian81
1. Bitte benutze demnächst die code-tags da man nämlich so ganz schlecht deinen Code lesen kann
2.

Code: Alles auswählen

connec (testAct, SIGNAL(triggered()), this, SLOT(slotTest(1)));
kann nicht funktionieren
3. Wenn amn Q_OBJECT auskommentiert kann es erst recht nichts mit Signals & Slots werden
4. was soll die selbstgebastelte itoa Funktion da? Schonmal in die Hilfe geschaut und QString::number() gesehen? char* in einem Qt Programm ... tztz :)

Verfasst: 5. Februar 2007 14:11
von waldo
Danke ersteinmal für die Hilfe, das mit dem auskommentieren des Q_OBJECT hab ich deshalb gemacht weil dann der Linker einige Objekte nicht auflösen kann, wahrscheinlich hab ich etwas vergessen in den Settings mit anzugeben, aber was ?
Meldung vom Linker ist:
1.
RandCPmp3.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall CRandCPmp3::metaObject(void)const " (?metaObject@CRandCPmp3@@UBEPBUQMetaObject@@XZ)
2.
RandCPmp3.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall CRandCPmp3::qt_metacast(char const *)" (?qt_metacast@CRandCPmp3@@UAEPAXPBD@Z)
3.
RandCPmp3.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CRandCPmp3::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@CRandCPmp3@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
4.
RandCPmp3.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const CRandCPmp3::staticMetaObject" (?staticMetaObject@CRandCPmp3@@2UQMetaObject@@B)


Das mit selbstgebauten itoa Funktion hatte ich gemacht weil ich den Code unter Windows + Linux compelieren wollte und nicht wußte das die QT Bibliotheken das ebenfalls abdecken (hatte halt nie danch gesucht;), danke für den Tip.
gruß

Verfasst: 5. Februar 2007 14:23
von Christian81
Das was der Linker anmeckert ist genau das was Du brauchst (aber auch schon zig mal hier besprochen).
Wenn Du qmake benutzt, musst Du qmake neu ausführen damit das .vcproj neu erstellt wird (nachdem Du unter HEADERS deine Headerdatei eingefügt hast). Wenn nicht, musst Du dich wohl oder übel mit dem moc beschäftigen. Eben alles von Hand. Hilfe gibt es von mir dazu nicht - entweder man benutzt qmake (oder cmake) und es funktioniert oder er muss sich mit moc (und uic) rumschlagen (was nichtmal ich mache da es zu umständlich ist)

Verfasst: 5. Februar 2007 14:55
von waldo
ok dann versuch ich mal ein bisserl mich mit dem qmake auseinander zu setzen. Ich danke dir für die schnelle und hilfreiche Hilfe.

gruß

p.s. tut mir leid wenn es zum x'ten mal angesprochen wurde.