我正在学习Qt4并完成他们的教程。

在本教程中:

http://doc.trolltech.com/4.5/mainwindows -menus-主窗口-cpp.html

他们有以下代码:

fileMenu = menuBar()->addMenu(tr("&File"));

导致编译器抛出此错误

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. -I. -o main.o main.cpp
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. -I. -o MainWindow.o MainWindow.cpp
MainWindow.cpp: In member function ‘void MainWindow::createMenus()’:
MainWindow.cpp:56: error: ‘((MainWindow*)this)->MainWindow::menuBar’ cannot be used as a function
MainWindow.cpp:61: error: ‘((MainWindow*)this)->MainWindow::menuBar’ cannot be used as a function
make: *** [MainWindow.o] Error 1

有谁知道如何解决这个问题?

[编辑]使用g ++添加完整错误消息

有帮助吗?

解决方案 2

由于某些原因,QMainWindow没有正确设置。这是通过调用基类构造函数来解决的。

其他提示

您确定要从 QMainWindow 继承,您还没有创建或继承可能影响名称 menuBar 的任何字段,并且您已经运行 moc (或 qmake 为你这样做)?

mainwindow.cpp mainwindow.h main.cpp menus.pro 来自例如,未经修改,应该可以正常工作。

$ cd examples/mainwindows/menus/
$ ls
main.cpp  mainwindow.cpp  mainwindow.h  menus.pro
$ qmake
$ make
g++ -c -pipe -g -O2 -mtune=native -pipe -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 mainwindow.o mainwindow.cpp
g++ -c -pipe -g -O2 -mtune=native -pipe -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 main.o main.cpp
/usr/bin/moc -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. mainwindow.h -o moc_mainwindow.cpp
g++ -c -pipe -g -O2 -mtune=native -pipe -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 moc_mainwindow.o moc_mainwindow.cpp
g++ -Wl,--as-needed -Wl,--hash-style=both -o menus mainwindow.o main.o moc_mainwindow.o    -L/usr/lib64/qt4 -lQtGui -L/usr/lib64 -L/usr/lib64/qt4 -L/usr/X11R6/lib64 -pthread -lpng -lfreetype -lgobject-2.0 -lSM -lICE -pthread -pthread -lXrender -lXrandr -lXinerama -lfontconfig -lXext -lX11 -lQtCore -lz -lm -pthread -lgthread-2.0 -lrt -lglib-2.0 -ldl -lpthread
$ ls -F
Makefile  main.o          mainwindow.h  menus*     moc_mainwindow.cpp
main.cpp  mainwindow.cpp  mainwindow.o  menus.pro  moc_mainwindow.o
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top