写出qt完整代码1. 实现一个简单计算器:可以实现整数的加减运算,要求控件的布局采用布局管理器来管理。(在实现运算的时候要注意QString类型与int类型数值之间的转换) 2. 创建一用户注册登录对话框模拟用户注册登录过程。要求验证用户名密码是否正确,验证失败信息用消息对话框提示。验证成功则重新打开一个新的对话框,该对话框上显示当前用户的姓名、学号、班级、地址等信息。 3. 实现一个简单的文本编辑器。要求: (1) 该窗口有标题栏、菜单栏和工具栏,请使用setwindowtittle()方法设置标题。 (2) 菜单栏有file和edit两个下拉菜单。File菜单下有新建、打开、退出三个action;Edit菜单下有复制、粘贴、剪切三个action。 (3) 创建一个edit工具栏,该工具栏包含保存、剪切、粘贴三个action。
时间: 2024-03-05 15:53:47 浏览: 109
Qt 教程代码 (http://c.biancheng.net/qt/)
好的,下面是Qt完整代码,包含三个部分,分别实现了三个功能:
1. 实现一个简单计算器
```
#include <QApplication>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 创建窗口和控件
QWidget *w = new QWidget;
QLabel *label1 = new QLabel("Number 1:");
QLabel *label2 = new QLabel("Number 2:");
QLineEdit *lineEdit1 = new QLineEdit;
QLineEdit *lineEdit2 = new QLineEdit;
QLabel *resultLabel = new QLabel("Result:");
QPushButton *addButton = new QPushButton("Add");
QPushButton *subButton = new QPushButton("Subtract");
// 创建布局管理器
QVBoxLayout *vLayout = new QVBoxLayout;
QHBoxLayout *hLayout1 = new QHBoxLayout;
QHBoxLayout *hLayout2 = new QHBoxLayout;
// 添加控件到布局管理器中
hLayout1->addWidget(label1);
hLayout1->addWidget(lineEdit1);
hLayout2->addWidget(label2);
hLayout2->addWidget(lineEdit2);
vLayout->addLayout(hLayout1);
vLayout->addLayout(hLayout2);
vLayout->addWidget(resultLabel);
vLayout->addWidget(addButton);
vLayout->addWidget(subButton);
w->setLayout(vLayout);
// 设置窗口标题
w->setWindowTitle("Calculator");
// 连接按钮的Click事件
QObject::connect(addButton, &QPushButton::clicked, [=](){
int num1 = lineEdit1->text().toInt();
int num2 = lineEdit2->text().toInt();
int result = num1 + num2;
resultLabel->setText("Result: " + QString::number(result));
});
QObject::connect(subButton, &QPushButton::clicked, [=](){
int num1 = lineEdit1->text().toInt();
int num2 = lineEdit2->text().toInt();
int result = num1 - num2;
resultLabel->setText("Result: " + QString::number(result));
});
// 显示窗口
w->show();
return a.exec();
}
```
2. 创建一用户注册登录对话框模拟用户注册登录过程
```
#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 创建主窗口和控件
QDialog *mainWin = new QDialog;
QLabel *userLabel = new QLabel("Username:");
QLabel *passLabel = new QLabel("Password:");
QLineEdit *userLineEdit = new QLineEdit;
QLineEdit *passLineEdit = new QLineEdit;
QPushButton *loginButton = new QPushButton("Login");
QPushButton *regButton = new QPushButton("Register");
// 创建布局管理器
QVBoxLayout *vLayout = new QVBoxLayout;
QHBoxLayout *hLayout1 = new QHBoxLayout;
QHBoxLayout *hLayout2 = new QHBoxLayout;
// 添加控件到布局管理器中
hLayout1->addWidget(userLabel);
hLayout1->addWidget(userLineEdit);
hLayout2->addWidget(passLabel);
hLayout2->addWidget(passLineEdit);
vLayout->addLayout(hLayout1);
vLayout->addLayout(hLayout2);
vLayout->addWidget(loginButton);
vLayout->addWidget(regButton);
mainWin->setLayout(vLayout);
// 设置窗口标题
mainWin->setWindowTitle("Login/Register");
// 连接按钮的Click事件
QObject::connect(loginButton, &QPushButton::clicked, [=](){
QString username = userLineEdit->text();
QString password = passLineEdit->text();
if(username == "admin" && password == "password"){
QDialog *userInfoWin = new QDialog;
QVBoxLayout *vLayout = new QVBoxLayout;
QLabel *nameLabel = new QLabel("Name: John");
QLabel *idLabel = new QLabel("ID: 123456");
QLabel *classLabel = new QLabel("Class: A1");
QLabel *addressLabel = new QLabel("Address: No.1 Road");
vLayout->addWidget(nameLabel);
vLayout->addWidget(idLabel);
vLayout->addWidget(classLabel);
vLayout->addWidget(addressLabel);
userInfoWin->setLayout(vLayout);
userInfoWin->setWindowTitle("User Information");
userInfoWin->exec();
}else{
QMessageBox::warning(mainWin, "Login Failed", "Invalid username or password!");
}
});
QObject::connect(regButton, &QPushButton::clicked, [=](){
QMessageBox::information(mainWin, "Register", "Registration is not supported in this demo!");
});
// 显示窗口
mainWin->show();
return a.exec();
}
```
3. 实现一个简单的文本编辑器
```
#include <QApplication>
#include <QMainWindow>
#include <QTextEdit>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QToolBar>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 创建主窗口和控件
QMainWindow *mainWin = new QMainWindow;
QTextEdit *textEdit = new QTextEdit;
QMenuBar *menuBar = new QMenuBar;
QMenu *fileMenu = new QMenu("File");
QMenu *editMenu = new QMenu("Edit");
QAction *newAction = new QAction("New");
QAction *openAction = new QAction("Open");
QAction *exitAction = new QAction("Exit");
QAction *copyAction = new QAction("Copy");
QAction *pasteAction = new QAction("Paste");
QAction *cutAction = new QAction("Cut");
QToolBar *toolBar = new QToolBar;
QAction *saveAction = new QAction("Save");
QAction *cutAction2 = new QAction("Cut");
QAction *pasteAction2 = new QAction("Paste");
// 添加控件到窗口中
mainWin->setCentralWidget(textEdit);
mainWin->setMenuBar(menuBar);
menuBar->addMenu(fileMenu);
menuBar->addMenu(editMenu);
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(exitAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
editMenu->addAction(cutAction);
mainWin->addToolBar(toolBar);
toolBar->addAction(saveAction);
toolBar->addAction(cutAction2);
toolBar->addAction(pasteAction2);
// 设置窗口标题
mainWin->setWindowTitle("Text Editor");
// 连接Action的Click事件
QObject::connect(newAction, &QAction::triggered, [=](){
textEdit->clear();
});
QObject::connect(openAction, &QAction::triggered, [=](){
QString filename = QFileDialog::getOpenFileName(mainWin, "Open File", ".", "Text Files(*.txt)");
if(filename != ""){
QFile file(filename);
if(file.open(QIODevice::ReadOnly)){
QTextStream stream(&file);
textEdit->setText(stream.readAll());
file.close();
}else{
QMessageBox::warning(mainWin, "Open Failed", "Failed to open file!");
}
}
});
QObject::connect(exitAction, &QAction::triggered, [=](){
mainWin->close();
});
QObject::connect(copyAction, &QAction::triggered, [=](){
textEdit->copy();
});
QObject::connect(pasteAction, &QAction::triggered, [=](){
textEdit->paste();
});
QObject::connect(cutAction, &QAction::triggered, [=](){
textEdit->cut();
});
QObject::connect(saveAction, &QAction::triggered, [=](){
QString filename = QFileDialog::getSaveFileName(mainWin, "Save File", ".", "Text Files(*.txt)");
if(filename != ""){
QFile file(filename);
if(file.open(QIODevice::WriteOnly)){
QTextStream stream(&file);
stream << textEdit->toPlainText();
file.close();
}else{
QMessageBox::warning(mainWin, "Save Failed", "Failed to save file!");
}
}
});
QObject::connect(cutAction2, &QAction::triggered, [=](){
textEdit->cut();
});
QObject::connect(pasteAction2, &QAction::triggered, [=](){
textEdit->paste();
});
// 显示窗口
mainWin->show();
return a.exec();
}
```
阅读全文