• 4962阅读
  • 7回复

[提问]创建菜单并连接它们的信号,出意外了。 [复制链接]

上一主题 下一主题
离线gameboy70949
 
只看楼主 倒序阅读 楼主  发表于: 2013-05-19
下面代码将创建菜单栏文件|退出帮助|关于帮助|关于Qt
  1. void MainWindow::CreatMenuBar(){
  2.     pmbMenuBar = new QMenuBar( this );
  3.     setMenuBar ( pmbMenuBar );
  4.     
  5.     QMenu * pqmFile, * pqmHelp;
  6.     QAction * pqaExit, * pqaAbout, * pqaAboutQt;
  7.     
  8.     pqmFile = new QMenu( tr("&File"), this );
  9.     pmbMenuBar->addMenu( pqmFile );
  10.     
  11.     pqaExit = pqmFile->addAction( tr("E&xit") );
  12.     pqaExit->setShortcut( tr("Ctrl+Q") );
  13.     connect( pqaExit, SIGNAL( triggered() ), this, SLOT( close() ) );
  14.     
  15.     pqmHelp = new QMenu( tr("&Help"), this);
  16.     pmbMenuBar->addMenu( pqmHelp );
  17.     
  18.     pqaAbout = pqmHelp->addAction( tr("&About") );
  19.     pqaAbout->setShortcut( tr("Ctrl+A") );
  20.     connect( pqaAbout, SIGNAL( triggered() ), this, SLOT( About() ) );
  21.     
  22.     pqaAboutQt = pqmHelp->addAction( tr("About &Qt") );
  23.     pqaAboutQt->setShortcut( tr("Ctrl+T") );
  24.     connect( pqaAboutQt, SIGNAL( triggered() ), this, SLOT( AboutQt() ) );
  25. }
  26. void MainWindow::About(){
  27.     QMessageBox::about (
  28.         this,
  29.         tr("About The Application"),
  30.         tr("<p>The <b>Qt Application</b> is my application with Qt.</p>")
  31.     );
  32. }
  33. void MainWindow::AboutQt(){
  34.     QMessageBox::aboutQt (
  35.         this,
  36.         tr("About Qt")
  37.     );
  38. }

现在问题是:点击文件|退出菜单之后程序真的退出了,这是符合期待的。但是,点击帮助|关于帮助|关于Qt这两个菜单,程序却没有反应,没有弹出相应的关于对话框,这是为什么呢?


离线gameboy70949
只看该作者 1楼 发表于: 2013-05-19


我错了,原来要在类中这样定义才可以:
  1. private slots:
  2. void About();
  3. void AboutQt();

我刚才又翻了一下例子才发现这个特别的地方。
各位作为个参考吧,刚才发了这帖子,又删不了,霸占地方了。。。。
离线jdwx

只看该作者 2楼 发表于: 2013-05-20
建议用QtCreator写代码,信号和槽会有正确的提示,声明写错了,不会出现在提示列表里。
void QApplication::aboutQt() [static slot]
QApplication有这么一个槽,直接和action connect就完成了AboutQt。
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线gameboy70949
只看该作者 3楼 发表于: 2013-05-20
回 2楼(jdwx) 的帖子
我现在弄了很久都没办法搞定那个按钮的单击信号:
  1. pqdbbButtonBox = new QDialogButtonBox( this );
  2.     pqlMainLayout->addWidget( pqdbbButtonBox );
  3.     pqpbEncryption = pqdbbButtonBox->addButton( tr("加密(&E)"), QDialogButtonBox::ActionRole );
  4.     pqpbDecryption = pqdbbButtonBox->addButton( tr("解密(&D)"), QDialogButtonBox::ActionRole );
  5.     connect( pqpbEncryption, SIGNAL( clicked() ), this, SLOT( Encryption() ) );
  6.     connect( pqpbDecryption, SIGNAL( clicked() ), this, SLOT( Decryption() ) );

  1. private slots:
  2.     void Encryption();
  3.     void Decryption();
  4. private:
  5.     QTextEdit * pqteText, * pqteCipherText;
  6.     QLineEdit * pqleKey;
  7.     QDialogButtonBox * pqdbbButtonBox;
  8.     QPushButton * pqpbEncryption, * pqpbDecryption;

编译失败,
  1. MainWindow.cpp: In member function 'void MainWindow::CreatLayouts()':
  2. MainWindow.cpp:74: error: no matching function for call to 'MainWindow::connect(QPushButton*&, const char*, MainWindow* const, const char*)'
  3. ..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qobject.h:204: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
  4. ..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qobject.h:217: note:                 static bool QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
  5. ..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qobject.h:337: note:                 bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
  6. MainWindow.cpp:75: error: no matching function for call to 'MainWindow::connect(QPushButton*&, const char*, MainWindow* const, const char*)'
  7. ..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qobject.h:204: note: candidates are: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
  8. ..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qobject.h:217: note:                 static bool QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
  9. ..\..\..\..\Qt\4.8.4\include/QtCore/../../src/corelib/kernel/qobject.h:337: note:                 bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
  10. mingw32-make[1]: *** [debug/MainWindow.o] Error 1
  11. mingw32-make: *** [debug] Error 2
我是按照着example/dialogs/standarddialogs/dialog.cpp来写的(虽然不尽相同),不知道它为什么总是说“未能找到一个适配的函数去调用connect。
它给出了三个函数原型,但是我不太看得出来我写的函数哪里错了。
离线jdwx

只看该作者 4楼 发表于: 2013-05-20
新建一个工程测试,你的代码除了:pqlMainLayout没有声明,其它的没问题。
清除所有编译出来的文件,包括makefile,再编译。
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
离线gameboy70949
只看该作者 5楼 发表于: 2013-05-20
回 4楼(jdwx) 的帖子
还是同样的错误,我把我的全部代码贴出来吧:
main.cpp
  1. #include "MainWindow.h"
  2. #include <QApplication>
  3. int main( int argc, char **argv){
  4.     QApplication qappMainApp( argc, argv );
  5.     MainWindow mwMyMainWindow;
  6.     mwMyMainWindow.show();
  7.     return qappMainApp.exec();
  8. }
MainWindow.cpp
  1. #include "MainWindow.h"
  2. MainWindow::MainWindow(){
  3.     QTextCodec::setCodecForTr( QTextCodec::codecForName( "UTF-8" ) );
  4.     pqlMainLayout = new QVBoxLayout( this );
  5.     setLayout( pqlMainLayout );
  6.     resize( 600, 400 );
  7.     CreatMenuBar();
  8.     CreatLayouts();
  9.     
  10. }
  11. MainWindow::~MainWindow(){
  12.     /*
  13.     delete pqlbHelloWorld;
  14.     setCentralWidget( NULL );
  15.     */
  16. }
  17. void MainWindow::CreatMenuBar(){
  18.     pmbMenuBar = new QMenuBar( this );
  19.     setMenuBar( pmbMenuBar );
  20.     
  21.     pqmFile = new QMenu( tr("文件(&F)"), this );
  22.     pmbMenuBar->addMenu( pqmFile );
  23.     
  24.     pqaExit = pqmFile->addAction( tr("退出(&X)") );
  25.     pqaExit->setShortcut( tr("Ctrl+Q") );
  26.     connect( pqaExit, SIGNAL( triggered() ), this, SLOT( close() ) );
  27.     
  28.     pqmHelp = new QMenu( tr("帮助(&H)"), this);
  29.     pmbMenuBar->addMenu( pqmHelp );
  30.     
  31.     pqaAbout = pqmHelp->addAction( tr("关于(&A)") );
  32.     pqaAbout->setShortcut( tr("Ctrl+A") );
  33.     connect( pqaAbout, SIGNAL( triggered() ), this, SLOT( About() ) );
  34.     
  35.     pqaAboutQt = pqmHelp->addAction( tr("关于Qt(&Q)") );
  36.     pqaAboutQt->setShortcut( tr("Ctrl+T") );
  37.     connect( pqaAboutQt, SIGNAL( triggered() ), this, SLOT( AboutQt() ) );
  38. }
  39. void MainWindow::CreatLayouts(){
  40.     pqwCentralWidget = new QWidget( this );
  41.     setCentralWidget( pqwCentralWidget );
  42.     pqlMainLayout = new QVBoxLayout( this );
  43.     pqwCentralWidget -> setLayout( pqlMainLayout );
  44.     
  45.     pqgbTextBox = new QGroupBox( tr("明文(&T)"), this );
  46.     pqlMainLayout->addWidget( pqgbTextBox );
  47.     pqlTextLayout = new QHBoxLayout();
  48.     pqgbTextBox->setLayout( pqlTextLayout );
  49.     pqteText = new QTextEdit;
  50.     pqlTextLayout->addWidget( pqteText );
  51.     
  52.     pqgbCipherTextBox = new QGroupBox( tr("密文(&C)"), this );
  53.     pqlMainLayout->addWidget( pqgbCipherTextBox );
  54.     pqlCipherTextLayout = new QHBoxLayout();
  55.     pqgbCipherTextBox->setLayout( pqlCipherTextLayout );
  56.     pqteCipherText = new QTextEdit;
  57.     pqlCipherTextLayout->addWidget( pqteCipherText );
  58.     
  59.     pqwKeyWidget = new QWidget();
  60.     pqlMainLayout->addWidget( pqwKeyWidget );
  61.     pqlKeyLayout = new QHBoxLayout();
  62.     pqwKeyWidget->setLayout( pqlKeyLayout );
  63.     pqlKeyLabel= new QLabel( tr("密钥(&K)") );
  64.     pqlKeyLayout->addWidget( pqlKeyLabel );
  65.     pqleKey = new QLineEdit();
  66.     pqlKeyLayout->addWidget( pqleKey );
  67.     
  68.     pqdbbButtonBox = new QDialogButtonBox( this );
  69.     pqlMainLayout->addWidget( pqdbbButtonBox );
  70.     pqpbEncryption = pqdbbButtonBox->addButton( tr("加密(&E)"), QDialogButtonBox::ActionRole );
  71.     pqpbDecryption = pqdbbButtonBox->addButton( tr("解密(&D)"), QDialogButtonBox::ActionRole );
  72.     connect( pqpbEncryption, SIGNAL( clicked() ), this, SLOT( Encryption() ) );
  73.     connect( pqpbDecryption, SIGNAL( clicked() ), this, SLOT( Decryption() ) );
  74. }
  75. void MainWindow::About(){
  76.     QMessageBox::about(
  77.         this,
  78.         tr("关于本程序"),
  79.         tr("<p>这<b>Qt</b>应用程序部分源代码使用了Qt库。</p>")
  80.     );
  81. }
  82. void MainWindow::AboutQt(){
  83.     QMessageBox::aboutQt(
  84.         this,
  85.         tr("关于 Qt")
  86.     );
  87. }
  88. void MainWindow::Encryption(){
  89.     QMessageBox::about(
  90.         this,
  91.         tr("关于本程序"),
  92.         tr("<p>你点击了加密按钮</p>")
  93.     );
  94. }
  95. void MainWindow::Decryption(){
  96.     QMessageBox::about(
  97.         this,
  98.         tr("关于本程序"),
  99.         tr("<p>你点击了解密按钮</p>")
  100.     );
  101. }
MainWindow.h
  1. #include <QMainWindow>
  2. #include <QLabel>
  3. #include <QGroupBox>
  4. #include <QAction>
  5. #include <QLayout>
  6. #include <QMenuBar>
  7. #include <QTextEdit>
  8. #include <QLineEdit>
  9. #include <QMessageBox>
  10. #include <QDialogButtonBox>
  11. #include <QTextCodec>
  12. class MainWindow : public QMainWindow{
  13.     Q_OBJECT
  14. public:
  15.     MainWindow(void);
  16.     ~MainWindow(void);
  17. protected:
  18.     
  19. private slots:
  20.     void About();
  21.     void AboutQt();
  22.     void Encryption();
  23.     void Decryption();
  24.     
  25. private:
  26.     void CreatMenuBar();
  27.     void CreatLayouts();
  28.     QWidget * pqwCentralWidget;
  29.     QLayout * pqlMainLayout;
  30.     QMenuBar * pmbMenuBar;
  31.     QMenu * pqmFile, * pqmHelp;
  32.     QAction * pqaExit, * pqaAbout, * pqaAboutQt;
  33.     QGroupBox * pqgbTextBox, * pqgbCipherTextBox;
  34.     QLayout * pqlTextLayout, * pqlCipherTextLayout, * pqlKeyLayout;
  35.     QWidget * pqwKeyWidget;
  36.     QLabel * pqlKeyLabel;
  37.     QTextEdit * pqteText, * pqteCipherText;
  38.     QLineEdit * pqleKey;
  39.     QDialogButtonBox * pqdbbButtonBox;
  40.     QPushButton * pqpbEncryption, * pqpbDecryption;
  41. };

然后cmd,cd /d ...,一次执行下面命令:
  1. qmake -project
  2. qmake
  3. make




离线gameboy70949
只看该作者 6楼 发表于: 2013-05-20
补充下:如果把那两句错误的代码屏蔽掉,程序就可以编译通过并运行正常。
离线jdwx

只看该作者 7楼 发表于: 2013-05-20
回 5楼(gameboy70949) 的帖子
只是缺少#include  <QPushButton>
发帖时要说明:操作系统、Qt版本、编译器,这样能更快的得到回复。
快速回复
限100 字节
 
上一个 下一个