ich bin in der Welt von C++ völlig neu und muss zugeben, da ich von der Programmiersprache D komme, dass es garnicht ganz so schwer ist, wie ich es dachte.
Aer nun zu meinem Problem^^
Und zwar wollte ich, da ich ja wie viele schreibfaul bin, eine Funktion bereitstellen, womit ich einfach Menu-Items einfügen kann.
Das ganze klappt aber nicht so, wie ich es mir wünsche
Denn ich bekomme folgende Fehler beim kompilieren:
Hier mal meine Dateien:mainwindow.cpp: In constructor `MyWin::MyWin(QWidget*)':
mainwindow.cpp:10: error: no matching function for call to `MyWin::addMenuItem(Q
Menu*&, const char[17], const char[5], const char[7], const char[24], void (MyWi
n::*)())'
mainwindow.h:19: note: candidates are: void MyWin::addMenuItem(QMenu*, char, cha
r, char, char, void (*)())
mainwindow.cpp: In member function `void MyWin::addMenuItem(QMenu*, char, char,
char, char, void (*)())':
mainwindow.cpp:19: error: call of overloaded `QIcon(char&)' is ambiguous
c:/Qt/4.4.1/include/QtGui/../../src/gui/image/qicon.h:72: note: candidates are:
QIcon::QIcon(QIconEngineV2*) <near match>
c:/Qt/4.4.1/include/QtGui/../../src/gui/image/qicon.h:71: note:
QIcon::QIcon(QIconEngine*) <near match>
c:/Qt/4.4.1/include/QtGui/../../src/gui/image/qicon.h:70: note:
QIcon::QIcon(const QString&) <near match>
c:/Qt/4.4.1/include/QtGui/../../src/gui/image/qicon.h:68: note:
QIcon::QIcon(const QPixmap&) <near match>
mainwindow.cpp:19: error: invalid conversion from `char' to `const char*'
mainwindow.cpp:19: error: initializing argument 1 of `static QString MyWin::tr
(const char*, const char*)'
mainwindow.cpp:20: error: invalid conversion from `char' to `const char*'
mainwindow.cpp:20: error: initializing argument 1 of `static QString MyWin::tr
(const char*, const char*)'
mainwindow.cpp:21: error: invalid conversion from `char' to `const char*'
mainwindow.cpp:21: error: initializing argument 1 of `static QString MyWin::tr
(const char*, const char*)'
NMAKE : U1077:
Stop.
NMAKE : U1077:
Stop.
main.cpp:
Code: Alles auswählen
#include "mainwindow.h"
#include <QApplication>
int main( int argc, char** argv )
{
QApplication app( argc, argv );
QWidget* window = new MyWin();
window->show();
return app.exec();
}
Code: Alles auswählen
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MyWin : public QMainWindow
{
Q_OBJECT
public:
MyWin( QWidget* = 0 );
public slots:
void newCon();
private:
void addMenuItem( QMenu*, char, char, char, char, void () );
};
#endif
Code: Alles auswählen
#include "mainwindow.h"
#include <QAction>
#include <QMenuBar>
#include <QMessageBox>
MyWin::MyWin( QWidget *parent )
: QMainWindow( parent, 0 )
{
QMenu* connectionMenu = new QMenu( this );
addMenuItem( connectionMenu, ":/../res/new.png", "&New", "Ctrl+N", "Create a new connection", &MyWin::newCon );
menuBar()->addMenu(connectionMenu)->setText("&Connection");
statusBar();
}
void MyWin::addMenuItem( QMenu* menu, char icon, char title, char shortcut, char statustip, void fn() )
{
QAction* action = new QAction( QIcon( icon ), tr( title ), this);
action->setShortcut( tr( shortcut ) );
action->setStatusTip( tr( statustip ) );
connect( action, SIGNAL( triggered() ), this, SLOT( fn() ) );
menu->addAction( action );
}
void MyWin::newCon()
{
QMessageBox::information( this, "action", "new connection selected" );
}
Mfg,
Danny.