QT学习之路12-14的源代码有些不完整,为了更好的让大家学习,本人做了一点修正与补充,谢谢。源代码如下:
头文件:
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QtGui/QMainWindow>
- #include <QLabel>
- #include <QStatusBar>
- class QAction;
- //public class QLabel;
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- MainWindow(QWidget *parent = 0);
- ~MainWindow();
- private slots:
- void open();
- private:
- QAction *openAction;
- QLabel *msgLabel;
- };
- #endif // MAINWINDOW_H
主源代码:mainwindow.cpp
- #include <QtGui/QAction>
- #include <QtGui/QMenu>
- #include <QtGui/QMenuBar>
- #include <QtGui/QKeySequence>
- #include <QtGui/QToolBar>
- #include <QMessageBox>
- //#include <QLabel>
- #include "mainwindow.h"
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- {
- openAction = new QAction(tr("&Open"), this);
- openAction->setShortcut(QKeySequence::Open);
- openAction->setStatusTip(tr("Open a file."));
- openAction->setIcon(QIcon(":/2.png"));
- connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
- QMenu *file = menuBar()->addMenu(tr("&File"));
- file->addAction(openAction);
- QToolBar *toolBar = addToolBar(tr("&File"));
- toolBar->addAction(openAction);
- msgLabel = new QLabel;
- msgLabel->setMinimumSize(msgLabel->sizeHint());
- msgLabel->setAlignment(Qt::AlignHCenter);
- statusBar()->addWidget(msgLabel);
- }
- void MainWindow::open()
- {
- QMessageBox::information(NULL, tr("Open"), tr("Open a file"));
- }
- MainWindow::~MainWindow()
- {
- }
主函数main.cpp
- #include <QtGui/QApplication>
- #include "mainwindow.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
图标文件依据原文制作使用
















