【Qt】2019-2020学期项目总结

一曲冷凌霜 提交于 2019-12-24 04:09:57

目录

 

整体项目截图

1.实现一个简单的登录界面

2.实现主窗口

3读取txt格式文件,生成树图目录

4项目所有代码如下:


整体项目截图

其中resource里面放的是背景图.jpg文件和生成目录树的.txt文件,这些文件可以自定义

1.实现一个简单的登录界面

功能:

①包含用户名、密码、文件目录、跳转界面按钮

②当点击一个按钮式时可以跳转到主界面mainwindow

效果如图:

                                                     

2.实现主窗口

功能:

①当点击菜单栏里的relogin时,可以重新返回登录界面更改用户名

②设置状态栏,在状态栏可以显示当前的时间以及用户名

③设置listwidget,实现数据的传递,左移所有数据,左移当前行,右移所有数据,右移当前行

④设置子界面,当点击按钮时跳转到子界面dialog,实现生成树图的功能

效果如图:

                                        

3读取txt格式文件,生成树图目录

功能:

①由主界面跳转得来,读取.txt文件,利用TreeWidget 实现生成文件夹目录树

效果如图:

                                            

4项目所有代码如下:

login.h

#ifndef LOGIN_H
#define LOGIN_H

#include <QDialog>
#include <QLineEdit>
#include <QGridLayout>
#include <QPushButton>
#include <QFileDialog>
#include "mainwindow.h"
#include <dialog.h>

namespace Ui {
class Login;
}

class Login : public QDialog
{
    Q_OBJECT

public:
    explicit Login(QWidget *parent = 0);
    ~Login();
    MainWindow * p_MainWindow;
private:
    Ui::Login * ui;
    QLineEdit * p_lineEdit;
    QGridLayout * p_MainLayOut;
    QPushButton * p_QPushButton;

private slots:
    void ShowFile();
    void on_buttonBox_accepted();
    void on_buttonBox_rejected();
    void on_buttonBox_clicked(QAbstractButton *button);
};

#endif // LOGIN_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include <QDateTime>
#include <QLabel>
#include <dialog.h>
#include <QListWidget>


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

    QString UserID;

private:
    Ui::MainWindow *ui;

    QTimer qtimer;
    Dialog *dialog;

    QLabel *ztlTime;
    QLabel *ztlTitle;
    QLabel *ztName;

private slots:
    void ExeClose();
    void TimerProc();
    void ReLogin();
    void moveItem(QListWidget *source,QListWidget *target);
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_4_clicked();
    void on_pushButton_3_clicked();
    void on_pushButton_5_clicked();
};

#endif // MAINWINDOW_H

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    QString count[100]={};
    QString sort[100][100] = {};
    int i=0;
    int k[10]={0};
private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H

 login.cpp

#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);

    this->setObjectName("MainWindow");
    //设置窗口背景
    setStyleSheet("QWidget#MainWindow{border-image:url(:new/prefix1/login.jpg);}");

    p_lineEdit = new QLineEdit(this);
    p_MainLayOut = new QGridLayout(this);
    p_QPushButton = new QPushButton(this);

    p_MainLayOut->addWidget(p_lineEdit, 0, 0);
    p_MainLayOut->addWidget(p_QPushButton, 0, 1);
    connect(p_QPushButton, SIGNAL(clicked()), this, SLOT(ShowFile()));
}

Login::~Login()
{
    delete ui;
}

void Login::ShowFile()
{
    QString FileName  = QFileDialog::getOpenFileName(0, "打开文件", "", "C++ Files(*.cpp)");
    p_lineEdit->setText(FileName);

}

void Login::on_buttonBox_accepted()
{
    p_MainWindow->UserID = ui->lineEdit_UserID->text();

}

void Login::on_buttonBox_rejected()
{
    p_MainWindow->UserID = "";
}

minwindow.cpp

#include "mainwindow.h"
#include "login.h"
#include "ui_mainwindow.h"
#include "dialog.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setObjectName("MainWindow");
    //设置窗口背景
    setStyleSheet("QWidget#MainWindow{border-image:url(:new/prefix1/cc_01.jpg);}");


    connect(ui->actionClose, SIGNAL(triggered()), this, SLOT(ExeClose()));
    connect(ui->actionRelogin, SIGNAL(triggered()), this, SLOT(ReLogin()));

    connect(&qtimer,SIGNAL(timeout()),SLOT(TimerProc()));
    qtimer.start(1000);


    ztlTime=new QLabel(this);
    ztlTitle=new QLabel(this);
    ztName=new QLabel(this);

    ui->statusBar->addWidget(ztlTime);
    ui->statusBar->addWidget(ztlTitle);
    ui->statusBar->addWidget(ztName);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::ExeClose()
{
    ui->statusBar->showMessage("Close MainWindow", 3000);
    close();
}

void MainWindow::TimerProc()
{
    ztlTitle->setText("UserID:"+QString(UserID));
    ztName->setText("20175104062 陈聪聪");
    ztlTime->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss zzz"));
}

void MainWindow::ReLogin()
{
    Login * LoginFrm = new Login(this);
    LoginFrm->p_MainWindow=this;
    LoginFrm->setModal(true);
    LoginFrm->exec();

    if (this->UserID != "")
    {
        this->setWindowTitle(QString("CurrentUser:") + QString(this->UserID));
        this->update();
        this->show();

    }
    else
    {
        this->close();
    }
}

void MainWindow::moveItem(QListWidget *source,QListWidget *target)
{
    if(source->currentItem()){
        QListWidgetItem *newItem=source->currentItem()->clone();
        target->addItem(newItem);
        target->setCurrentItem(newItem);
        delete source->currentItem();
    }
}


void MainWindow::on_pushButton_clicked()
{
    int j=ui->listWidget->count();
    for(int i=0;i<j;i++){
        moveItem(ui->listWidget,ui->listWidget_2);
    }
}


void MainWindow::on_pushButton_2_clicked()
{
    moveItem(ui->listWidget,ui->listWidget_2);
}


void MainWindow::on_pushButton_3_clicked()
{
    int m=ui->listWidget_2->count();
    for(int i=0;i<m;i++){
        moveItem(ui->listWidget_2,ui->listWidget);
    }
}


void MainWindow::on_pushButton_4_clicked()
{
    moveItem(ui->listWidget_2,ui->listWidget);
}

void MainWindow::on_pushButton_5_clicked()
{
    dialog = new Dialog(this);
    dialog->show();
}

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include "login.h"
#include "dialog.h"
#include "QMessageBox"
#include "QListWidget"

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

    ///*
    Login * LoginFrm = new Login();
    LoginFrm->p_MainWindow = &w;
    LoginFrm->setModal(true);
    LoginFrm->exec();
    //*/

    if (w.UserID != "")
    {
        w.setWindowTitle(w.windowTitle() + QString("  CurrentUser:") + QString(w.UserID));
        //QMessageBox::information(NULL, "提示", w.windowTitle());
        w.update();
        w.show();
        return a.exec();
    }
    else
    {
        w.close();
    }
}

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include"QFile"
#include"QMessageBox"
#include"QTextStream"
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    this->setObjectName("MainWindow");
    //设置窗口背景
    setStyleSheet("QWidget#MainWindow{border-image:url(:new/prefix1/cc_02.jpg);}");
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked()
{
    QFile f(":/TreeData.txt");
    if(!f.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        QMessageBox::about(NULL,"文件","文件打开失败");
    }
    QTextStream txtInput(&f);
        QString A;


    while (!txtInput.atEnd())
    {
        count[i] = txtInput.readLine()+"\n";
        A=A+count[i];
        i++;
    }
     ui->textEdit->setPlainText(A);

    int k[10]={0};
    for(int j=0;j<i;j++)
    {
        if(count[j].left(2)=="01")
        {
            sort[j][k[0]]=count[j];
            k[0]++;
        }
        if(count[j].left(2)=="02")
        {
            sort[j][k[1]]=count[j];
        }
        if(count[j].left(2)=="03")
        {
            sort[j][k[2]]=count[j];
        }
        if(count[j].left(2)=="04")
        {
            sort[j][k[3]]=count[j];
        }


     f.close();
   }
}
void Dialog::on_pushButton_2_clicked()
{
    ui->treeWidget->setColumnCount(1);
    for(int n=0;n<i;n++)
    {
        QTreeWidgetItem *ItemA;
        QTreeWidgetItem *ItemB;
        QTreeWidgetItem *ItemC;
        QTreeWidgetItem *ItemD;
        int T=count[n].count();
        if(T==3)
        {
            ItemA= new QTreeWidgetItem(ui->treeWidget,QStringList(QString(count[n])));
        }
        if(T==5)
        {
            ItemB = new QTreeWidgetItem(ItemA,QStringList(QString(count[n])));
            ItemA->addChild(ItemB);
        }
        if(T==7)
        {
            ItemC = new QTreeWidgetItem(ItemB,QStringList(QString(count[n])));
            ItemB->addChild(ItemC);
        }
        if(T==9)
        {
            ItemD = new QTreeWidgetItem(ItemC,QStringList(QString(count[n])));
            ItemC->addChild(ItemD);
        }
    }
}

login.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Login</class>
 <widget class="QDialog" name="Login">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QDialogButtonBox" name="buttonBox">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>240</y>
     <width>341</width>
     <height>32</height>
    </rect>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="standardButtons">
    <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>50</y>
     <width>61</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>用户名</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_UserID">
   <property name="geometry">
    <rect>
     <x>160</x>
     <y>47</y>
     <width>113</width>
     <height>20</height>
    </rect>
   </property>
   <property name="text">
    <string>Test</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_Pass">
   <property name="geometry">
    <rect>
     <x>160</x>
     <y>77</y>
     <width>113</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>80</y>
     <width>51</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>密码</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>buttonBox</sender>
   <signal>accepted()</signal>
   <receiver>Login</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>248</x>
     <y>254</y>
    </hint>
    <hint type="destinationlabel">
     <x>157</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>buttonBox</sender>
   <signal>rejected()</signal>
   <receiver>Login</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>316</x>
     <y>260</y>
    </hint>
    <hint type="destinationlabel">
     <x>286</x>
     <y>274</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>898</width>
    <height>563</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QListWidget" name="listWidget">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>120</y>
      <width>256</width>
      <height>192</height>
     </rect>
    </property>
    <item>
     <property name="text">
      <string>1</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>3</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>5</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>7</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>9</string>
     </property>
    </item>
   </widget>
   <widget class="QListWidget" name="listWidget_2">
    <property name="geometry">
     <rect>
      <x>530</x>
      <y>130</y>
      <width>256</width>
      <height>192</height>
     </rect>
    </property>
    <item>
     <property name="text">
      <string>2</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>4</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>6</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>8</string>
     </property>
    </item>
    <item>
     <property name="text">
      <string>10</string>
     </property>
    </item>
   </widget>
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>220</x>
      <y>340</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>A</string>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>660</x>
      <y>340</y>
      <width>54</width>
      <height>12</height>
     </rect>
    </property>
    <property name="text">
     <string>B</string>
    </property>
   </widget>
   <widget class="QSplitter" name="splitter">
    <property name="geometry">
     <rect>
      <x>410</x>
      <y>170</y>
      <width>75</width>
      <height>92</height>
     </rect>
    </property>
    <property name="orientation">
     <enum>Qt::Vertical</enum>
    </property>
    <widget class="QPushButton" name="pushButton">
     <property name="text">
      <string>A&gt;&gt;B</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton_2">
     <property name="text">
      <string>A&gt;B</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton_3">
     <property name="text">
      <string>A&lt;&lt;B</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton_4">
     <property name="text">
      <string>A&lt;B</string>
     </property>
    </widget>
   </widget>
   <widget class="QPushButton" name="pushButton_5">
    <property name="geometry">
     <rect>
      <x>400</x>
      <y>370</y>
      <width>101</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string>打开子界面</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>898</width>
     <height>26</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuFile">
    <property name="title">
     <string>File</string>
    </property>
    <widget class="QMenu" name="menuOpen">
     <property name="title">
      <string>Open</string>
     </property>
    </widget>
    <addaction name="menuOpen"/>
    <addaction name="actionSave_2"/>
    <addaction name="separator"/>
    <addaction name="actionRelogin"/>
   </widget>
   <widget class="QMenu" name="menuClose">
    <property name="title">
     <string>Close</string>
    </property>
    <addaction name="actionClose"/>
   </widget>
   <addaction name="menuFile"/>
   <addaction name="menuClose"/>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
  <action name="actionSave_2">
   <property name="text">
    <string>Save</string>
   </property>
  </action>
  <action name="actionRelogin">
   <property name="text">
    <string>ReLogin</string>
   </property>
  </action>
  <action name="actionClose">
   <property name="text">
    <string>Close</string>
   </property>
  </action>
  <action name="actionOpen_the_TreeWindow">
   <property name="text">
    <string>Open the  TreeWindow</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

dialog.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>910</width>
    <height>714</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QTextEdit" name="textEdit">
   <property name="geometry">
    <rect>
     <x>130</x>
     <y>130</y>
     <width>251</width>
     <height>371</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton_2">
   <property name="geometry">
    <rect>
     <x>530</x>
     <y>530</y>
     <width>101</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>生成树图</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>180</x>
     <y>530</y>
     <width>121</width>
     <height>31</height>
    </rect>
   </property>
   <property name="text">
    <string>读取txt文件</string>
   </property>
  </widget>
  <widget class="QTreeWidget" name="treeWidget">
   <property name="geometry">
    <rect>
     <x>450</x>
     <y>130</y>
     <width>281</width>
     <height>371</height>
    </rect>
   </property>
   <column>
    <property name="text">
     <string notr="true">1</string>
    </property>
   </column>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!