QT练手小项目一

    最近在做MFC方面的工作,但是觉得QT不能够扔下,所以空闲的时候做一些QT方面的练手小项目,就当复习了。代码量很小,易于理解。我会把整个代码放在博客最后面。

         我们先来看一看整个页面,整个页面是比较简单的,有五个按钮和两个文本框。这只是一个空的界面,并没有做任何功能。

         我们先来看看MainWIndow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextEdit>
#include <QPushButton>
#include <QDialog>

class MainWindow : public QDialog
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QTextEdit *m_topPage;
    QTextEdit *m_belowPage;
    QTextEdit *m_rightPage;

    QPushButton *m_fontButton;
    QPushButton *m_sizeButton;
    QPushButton *m_logButton;
    QPushButton *m_closeButton;
    QPushButton *m_sendButton;
};

#endif // MAINWINDOW_H

      整个页面主要是用对话框的形式做出来的,所以继承的是QDialog.

     首先在类里面我们定义了对话框的基本部件,包括三个文本框,五个按钮,非常简单。

      下面我们就来看看MainWindow.cpp

       我首先对基本框非常简单的设置,就是设置大小和添加标题。

       resize(800, 600);
       setWindowTitle(tr("Dialog Exercise"));

        然后是初始化文本框和按钮。因为想看看所有的字体长什么样,所以每个按钮我都使用了不同的字体。

  m_topPage = new QTextEdit();
    m_belowPage = new QTextEdit();
    m_rightPage = new QTextEdit();

    //下面五个是五个按钮初始化
    m_fontButton = new QPushButton(tr("Font"));
    m_fontButton->setFont(QFont("Times", 18,
                                QFont::Black, true));
    m_sizeButton = new QPushButton(tr("Size"));
    m_sizeButton->setFont(QFont("Times", 18,
                                QFont::Bold, true));
    m_logButton = new QPushButton(tr("Log"));
    m_logButton->setFont(QFont("Times", 18,
                               QFont::Light, true));
    m_closeButton = new QPushButton(tr("Close"));
    m_closeButton->setFont(QFont("Times", 18,
                               QFont::Normal, true));

    m_sendButton = new QPushButton(tr("Send"));
    m_sendButton->setFont(QFont("Times", 18,
                                QFont::DemiBold, true));

    在这里我做了非常简单的一个connect,就是close按钮做了一个点击退出界面功能。其他的按钮就没有做SLOT

    connect(m_closeButton, SIGNAL(clicked()),
            qApp, SLOT(quit()));

     接下来是排列整个界面,我先排列的是左边的框架。因为我没排列好一部分框架就做了一个显示去查看效果,所以大家可以看到我注释了好几个setLayout()函数。

//横向排列好三个按钮
    QHBoxLayout *topButtonLayout = new QHBoxLayout();
    topButtonLayout->addWidget(m_fontButton);
    topButtonLayout->addWidget(m_sizeButton);
    topButtonLayout->addWidget(m_logButton);
    //setLayout(topButtonLayout);

    //纵向排列好按钮和字符界面
    QVBoxLayout *topTextLayout = new QVBoxLayout();
    topTextLayout->addWidget(m_topPage);
    topTextLayout->addLayout(topButtonLayout);
    //setLayout(topTextLayout);

    //横向排列下面的两个按钮
    QHBoxLayout *belowButtonLayout = new QHBoxLayout();
    belowButtonLayout->addWidget(m_closeButton);
    belowButtonLayout->addWidget(m_sendButton);

    //纵向排列好下面的字符界面
    QVBoxLayout *belowTextLayout = new QVBoxLayout();
    belowTextLayout->addWidget(m_belowPage);
    belowTextLayout->addLayout(belowButtonLayout);

    //纵向排列好左边的界面
    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->addLayout(topTextLayout);
    leftLayout->addLayout(belowTextLayout);
    //setLayout(leftLayout);

    左边的框架排列好以后,因为右边只有一个文本框,所以我们直接排列整个框架。直接排列的话,是对半分整个界面,不好看。所在排列之前我对右边的文本框进行了宽度设置。

  //右边的字符界面好像有点大,调整一下宽度
    m_rightPage->setMaximumWidth(300);

    //横向排列好整个界面
    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->addLayout(leftLayout);
    mainLayout->addWidget(m_rightPage);
    setLayout(mainLayout);

    主要代码到这里就已经完成了。也就是说整个界面我们已经排列好了。如果想要添加其他的功能可以在类里面声明几个SLOT,做connect。

    下面我把我的整个源码放在最下面,供初学者使用。

    main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

     MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextEdit>
#include <QPushButton>
#include <QDialog>

class MainWindow : public QDialog
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QTextEdit *m_topPage;
    QTextEdit *m_belowPage;
    QTextEdit *m_rightPage;

    QPushButton *m_fontButton;
    QPushButton *m_sizeButton;
    QPushButton *m_logButton;
    QPushButton *m_closeButton;
    QPushButton *m_sendButton;
};

#endif // MAINWINDOW_H


    MainWindow.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QFont>
#include <QHBoxLayout>
#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
    QDialog(parent)
{
    //设置界面大小以及抬头
    resize(800, 600);
    setWindowTitle(tr("Dialog Exercise"));

    //这三个是字符界面初始化
    m_topPage = new QTextEdit();
    m_belowPage = new QTextEdit();
    m_rightPage = new QTextEdit();

    //下面五个是五个按钮初始化
    m_fontButton = new QPushButton(tr("Font"));
    m_fontButton->setFont(QFont("Times", 18,
                                QFont::Black, true));
    m_sizeButton = new QPushButton(tr("Size"));
    m_sizeButton->setFont(QFont("Times", 18,
                                QFont::Bold, true));
    m_logButton = new QPushButton(tr("Log"));
    m_logButton->setFont(QFont("Times", 18,
                               QFont::Light, true));
    m_closeButton = new QPushButton(tr("Close"));
    m_closeButton->setFont(QFont("Times", 18,
                               QFont::Normal, true));
    //做一个关闭按钮
    connect(m_closeButton, SIGNAL(clicked()),
            qApp, SLOT(quit()));

    m_sendButton = new QPushButton(tr("Send"));
    m_sendButton->setFont(QFont("Times", 18,
                                QFont::DemiBold, true));

    //横向排列好三个按钮
    QHBoxLayout *topButtonLayout = new QHBoxLayout();
    topButtonLayout->addWidget(m_fontButton);
    topButtonLayout->addWidget(m_sizeButton);
    topButtonLayout->addWidget(m_logButton);
    //setLayout(topButtonLayout);

    //纵向排列好按钮和字符界面
    QVBoxLayout *topTextLayout = new QVBoxLayout();
    topTextLayout->addWidget(m_topPage);
    topTextLayout->addLayout(topButtonLayout);
    //setLayout(topTextLayout);

    //横向排列下面的两个按钮
    QHBoxLayout *belowButtonLayout = new QHBoxLayout();
    belowButtonLayout->addWidget(m_closeButton);
    belowButtonLayout->addWidget(m_sendButton);

    //纵向排列好下面的字符界面
    QVBoxLayout *belowTextLayout = new QVBoxLayout();
    belowTextLayout->addWidget(m_belowPage);
    belowTextLayout->addLayout(belowButtonLayout);

    //纵向排列好左边的界面
    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->addLayout(topTextLayout);
    leftLayout->addLayout(belowTextLayout);
    //setLayout(leftLayout);

    //右边的字符界面好像有点大,调整一下宽度
    m_rightPage->setMaximumWidth(300);

    //横向排列好整个界面
    QHBoxLayout *mainLayout = new QHBoxLayout();
    mainLayout->addLayout(leftLayout);
    mainLayout->addWidget(m_rightPage);
    setLayout(mainLayout);

}

MainWindow::~MainWindow()
{

}

猜你喜欢

转载自blog.csdn.net/dzhongjie/article/details/83832474