Mooc 课程:程序设计基础——发现计算之美
李骏扬 、魏海坤 、仰燕兰 、朱蔚萍 、杨万扣
网址:https://www.icourse163.org/course/SEU-1003771004
复数类
详见我的博客 【C++ 程序】 复数类 (Complex)和 【C++ 程序】 TVJ Complex Calculator (v 2.2) 复数计算器。
注册页面
(题目本来是说登陆界面,但我认为注册界面形容更恰当一些)
下面使用 Qt 制作。
编译环境
- Qt 6.0.0
- Qt 5 Compatibility Module
- MinGW 8.1.0 64-bit
- Qt Creator 4.14.0 (Community)
- Windows 10
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 <QMessageBox>
#include <QTableView>
#include <QVector>
#include <QStandardItemModel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class User_info;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QVector<User_info>* user_info_all = new QVector<User_info>();
QStandardItemModel* model = new QStandardItemModel();
private slots:
void on_actionExit_triggered();
void on_pushButton_clear_clicked();
void on_pushButton_signup_clicked();
private:
Ui::MainWindow *ui;
void clear_line();
};
class User_info
{
public:
User_info(QString user, QString pass, QString ph, QString gen = "Male");
QString User_name;
QString Pass_word;
QString Phone;
QString Gender;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
class User_info;
setWindowTitle("Sign up - Teddy van Jerry");
model->setColumnCount(4);
model->setHeaderData(0,Qt::Horizontal, "User name");
model->setHeaderData(1,Qt::Horizontal, "Pass word");
model->setHeaderData(2,Qt::Horizontal, "Phone Number");
model->setHeaderData(3,Qt::Horizontal, "Gender");
ui->tableView_users->setModel(model);
}
MainWindow::~MainWindow()
{
delete ui;
}
User_info::User_info(QString user, QString pass, QString ph, QString gen)
: User_name(user)
, Pass_word(pass)
, Phone(ph)
, Gender(gen)
{
QMessageBox::information(NULL, "Information", "Sign up successfully!");
}
void MainWindow::on_actionExit_triggered()
{
close();
}
void MainWindow::clear_line()
{
ui->line_Username->clear();
ui->line_Password->clear();
ui->line_Phonenumber->clear();
ui->line_Username->setFocus();
}
void MainWindow::on_pushButton_clear_clicked()
{
clear_line();
}
void MainWindow::on_pushButton_signup_clicked()
{
User_info this_info(
ui->line_Username->text(),
ui->line_Password->text(),
ui->line_Phonenumber->text(),
ui->comboBox_Gender->currentText()
);
user_info_all->push_back(this_info);
model->setItem(MainWindow::user_info_all->size() - 1, 0, new QStandardItem(ui->line_Username->text()));
model->setItem(MainWindow::user_info_all->size() - 1, 1, new QStandardItem(ui->line_Password->text()));
model->setItem(MainWindow::user_info_all->size() - 1, 2, new QStandardItem(ui->line_Phonenumber->text()));
model->setItem(MainWindow::user_info_all->size() - 1, 3, new QStandardItem(ui->comboBox_Gender->currentText()));
clear_line();
}
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>734</width>
<height>601</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout" columnstretch="2,3,0,0">
<item row="0" column="0" colspan="4">
<widget class="QLabel" name="label">
<property name="font">
<font>
<family>Agency FB</family>
<pointsize>48</pointsize>
</font>
</property>
<property name="text">
<string>User information</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="0">
<layout class="QGridLayout" name="gridLayout_2">
<item row="14" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0" colspan="3">
<widget class="QLabel" name="label_3">
<property name="text">
<string>User name:</string>
</property>
</widget>
</item>
<item row="14" column="2">
<widget class="QPushButton" name="pushButton_signup">
<property name="text">
<string>Sign up</string>
</property>
</widget>
</item>
<item row="12" column="0" colspan="3">
<widget class="QComboBox" name="comboBox_Gender">
<item>
<property name="text">
<string>Male</string>
</property>
</item>
<item>
<property name="text">
<string>Female</string>
</property>
</item>
</widget>
</item>
<item row="9" column="0" colspan="3">
<widget class="QLineEdit" name="line_Phonenumber"/>
</item>
<item row="10" column="2">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="6" column="0" colspan="3">
<widget class="QLineEdit" name="line_Password">
<property name="echoMode">
<enum>QLineEdit::PasswordEchoOnEdit</enum>
</property>
</widget>
</item>
<item row="0" column="0" colspan="3">
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<family>Sitka Heading</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="text">
<string>Sign up</string>
</property>
</widget>
</item>
<item row="7" column="2">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="14" column="0">
<widget class="QPushButton" name="pushButton_clear">
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
<item row="13" column="2">
<spacer name="verticalSpacer_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="11" column="0" colspan="3">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Gender</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="3">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Pass word:</string>
</property>
</widget>
</item>
<item row="8" column="0" colspan="3">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Phone Number</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="3">
<widget class="QLineEdit" name="line_Username"/>
</item>
<item row="4" column="2">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="1" column="1" colspan="2">
<widget class="QTableView" name="tableView_users"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>734</width>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menuWindow_W">
<property name="title">
<string>Window(&W)</string>
</property>
<addaction name="actionExit"/>
</widget>
<addaction name="menuWindow_W"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionExit">
<property name="text">
<string>Exit</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
效果
这个程序我核心试验的就是QTableView。
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。