#include "dialog.h" #include "ui_dialog.h" #include "student.h" #include "studentwork.h" #include <studentmanager.h> #include <QFile> #include <QTextCodec> #include <QTimer> #include <QCoreApplication> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); ui->lineEditPassword->setEchoMode(QLineEdit::Password); setWindowTitle("Login"); } Dialog::~Dialog() { delete ui; } void Dialog::on_buttonLogin_clicked() { student* newStudent = new student; static int count=0; QString ID=ui->lineEditPassword->text(); auto it = studentManager::m_students.find(ID); QString filePath = QCoreApplication::applicationDirPath() + "/student.txt"; QFile file(filePath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Could not open file" << filePath; return; } QTextStream in(&file); while (!in.atEnd()&&ui->lineEditID->text()!=newStudent->m_name) { QString line = in.readLine(); if(line == "end"){ break; } QStringList data = line.split(" "); newStudent->m_name = data[0]; newStudent->m_ID = data[1]; if(ui->lineEditID->text()==newStudent->m_name){ break; } } if (ui->lineEditPassword->text()==newStudent->m_ID) { accept(); } else { count++; qDebug()<<count; if(count>2) { this->close(); // 3次错误退出 } else { /* QMessageBox::warning(NULL,"warning",newStudent->m_name+newStudent->m_ID);*/ QMessageBox::warning(NULL, "warning","Account password error,please login again.you have "+QString::number(3-count)+" chances"); } } file.close(); }
时间: 2023-12-06 16:43:24 浏览: 30
这是一个登录界面的代码,通过输入用户名和密码来进行登录,其中使用了student类和studentManager类来进行数据管理。文件读取使用了QFile和QTextStream类。在点击登录按钮时,会读取文件中的内容,并将用户名和密码与输入的内容进行比对,如果一致则登录成功,否则会有三次机会重新输入密码,超过三次会强制退出程序。
相关问题
#include "imgui.h" #include "extgraph.h" #include "editor.h" #include "commandLine.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <winuser.h>仅用这些头文件实现在MessageBox弹出的弹窗里点击下一页,更新弹窗内容,只有一个弹窗,如何实现
首先,我们需要在MessageBox弹窗中添加一个按钮,用于触发下一页的更新操作。由于MessageBox函数本身是一个阻塞函数,无法进行更新操作,因此我们需要使用自定义的窗口来实现弹窗。
下面是一个简单的实现思路:
1. 定义一个全局变量来记录当前弹窗的页数。
```
int page = 1;
```
2. 创建一个自定义的窗口,用于显示弹窗内容。在窗口中添加一个按钮,用于触发下一页的更新操作。
```
void MyWindow() {
static int buttonPressed = 0;
ImGui::Begin("My Popup");
ImGui::Text("Page %d", page);
if (ImGui::Button("Next Page")) {
page++;
buttonPressed = 1;
}
ImGui::End();
if (buttonPressed) {
buttonPressed = 0;
MessageBox(NULL, "Popup updated!", "Info", MB_OK);
}
}
```
3. 在程序中调用MessageBox函数,并将其父窗口设置为自定义的窗口。
```
int main() {
int ret = MessageBox(NULL, "Click 'OK' to open popup.", "Info", MB_OKCANCEL);
if (ret == IDOK) {
HWND hwnd = (HWND)ImGui::GetMainViewport()->PlatformHandle;
int result = (int)DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(1), hwnd, NULL);
}
return 0;
}
```
4. 定义对话框过程函数,在其中创建自定义窗口并显示。
```
INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_INITDIALOG:
ImGui_ImplWin32_Init(hwndDlg);
break;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK) {
EndDialog(hwndDlg, 0);
return TRUE;
}
break;
case WM_PAINT:
ImGui_ImplWin32_NewFrame();
MyWindow();
ImGui::Render();
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
break;
case WM_CLOSE:
EndDialog(hwndDlg, 0);
break;
default:
return FALSE;
}
return TRUE;
}
```
在上述代码中,我们使用了ImGui库来创建自定义窗口,并在其中添加了一个按钮用于触发下一页的更新操作。在按钮被点击时,我们将buttonPressed变量设置为1,在消息循环中检测到该变量有变化后,调用MessageBox函数更新弹窗内容。
需要注意的是,由于MessageBox函数是一个阻塞函数,我们需要在程序中使用对话框来实现自定义窗口,并在其中显示弹窗内容。同时,我们需要在程序中调用ImGui_ImplWin32_Init函数来初始化ImGui库,并在消息循环中调用MyWindow函数来显示自定义窗口。
dialog.h:6:10: error: 'QTcpSocket' file not found
这个错误通常是由于缺少QTcpSocket库文件或者QTcpSocket库文件没有被正确地包含在你的项目中。你可以检查一下以下几个方面:
1. 确保你已经正确地安装了QTcpSocket库。
2. 确保你已经在你的项目文件中正确地包含了QTcpSocket库。你可以在.pro文件中添加以下行:
QT += network
3. 确保你在你的源代码文件中正确地包含了QTcpSocket头文件。你可以在你的源代码文件中添加以下行:
#include <QTcpSocket>
如果你已经做了以上几个步骤,并且还是出现了这个错误,那么你可以尝试重新编译你的项目,或者尝试在其他编译环境中编译项目。