只是想让这个工作,我不明白为什么它不起作用。在Windows 7 x64上使用Qt Creator编译,但我认为这不应该太重要。
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class QAction;
class QMenu;
class QWidget;
class mywidget;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
private:
void createActions();
void createMenus();
mywidget *thewidget;
QMenu *viewMenu;
QAction *toggleViewAct;
};
#endif
mywidget.h:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class mywidget : public QWidget
{
public:
mywidget(QWidget *parent){ m_parent = parent ; }
private slots:
void toggleView(){ isHidden() ? setVisible(true) : setVisible(false) ; }
private:
QWidget *m_parent;
};
#endif
mainwindow.cpp:
#include <QtWidgets>
#include "mainwindow.h"
#include "mywidget.h"
MainWindow::MainWindow()
{
thewidget = new mywidget(new QWidget());
thewidget->setWindowTitle("test");
thewidget->setVisible(true);
createActions();
createMenus();
}
void MainWindow::createActions()
{
toggleViewAct = new QAction(tr("Toggle other widget"), this);
toggleViewAct->setStatusTip(tr("toggle tabbed widgets visibility"));
connect(toggleViewAct, SIGNAL(triggered()), thewidget, SLOT(toggleView()));
}
void MainWindow::createMenus()
{
viewMenu = menuBar()->addMenu(tr("&Menu"));
viewMenu->addAction(toggleViewAct);
}
main.cpp中:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setOrganizationName("QtProject");
app.setApplicationName("Application Example");
MainWindow mainWin;
mainWin.show();
return app.exec();
}