免责声明:我是QT的新手,但是他还没有学习。
我正在创建一个多窗口QT项目,其中窗口需要相互通信。在创建这个项目期间,我不确定如何允许窗口相互通信。幸运的是,我找到了这个链接: Passing Data Between Windows
尝试在我的代码中实现它之后,我发现它没有响应,并且在编译后它不会运行。
我认为问题出在下面的代码中。
homeWindow::homeWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::homeWindow)
{
ui->setupUi(this);
/* Initialize Windows */
loginWindow = new login;
trainerW = new trainer;
// Show the login window
loginWindow->setFixedSize(400, 400);
loginWindow->show();
// Hide the current window
this->setVisible(false);
// Connect the calling of loginRequest in the loginWindow object to the visibility of the home window
connect(loginWindow, SIGNAL(loginRequest()), this, SLOT(setVisible(true)));
}
signals:
void loginRequest();
#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QStatusBar>
void login::on_pushButton_login_clicked()
{
QString username = ui->lineEdit_username->text();
QString password = ui->lineEdit_password->text();
if(username == "Test" && password == "test"){
loginRequest();
}else{
ui->label_messegeBar->setText("Username or password is wrong.");
}
}
我应该使用更好的解决方案吗?我对所有反馈持开放态度。
感谢大家的帮助,大宏小贩
我仍然遇到启动该计划的问题。我做了一些改变。我决定从相关文件中添加我的所有代码,以防出现我不知道的问题。
#include <QApplication>
#include "homewindow.h"
int mode = 1;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Create homewindow object, calls login in constructer
homeWindow hWindow;
hWindow.start();
return a.exec();
}
#ifndef HOMEWINDOW_H
#define HOMEWINDOW_H
#include <QMainWindow>
#include <trainer.h>
#include <login.h>
namespace Ui {
class homeWindow;
}
class homeWindow : public QMainWindow
{
Q_OBJECT
public:
explicit homeWindow(QWidget *parent = 0);
~homeWindow();
void setUserName(QString username);
void getTrainerData();
void start();
private slots:
void on_pushButton_learn_clicked();
private:
login *loginWindow;
Ui::homeWindow *ui;
QString userName;
trainer *trainerW;
};
#endif // HOMEWINDOW_H
#include "homewindow.h"
#include "ui_homewindow.h"
#include "trainer.h"
#include "session_adjust.h"
#include "login.h"
#include <QDebug>
homeWindow::homeWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::homeWindow)
{
ui->setupUi(this);
/* Initialize Windows */
loginWindow = new login;
trainerW = new trainer;
// Hide the current window
setVisible(false);
// Connect the calling of loginRequest in the loginWindow object to the visibility of the home window
connect (loginWindow, &login::loginRequest, this, &homeWindow::setVisible);
}
homeWindow::~homeWindow()
{
delete ui;
}
void homeWindow::setUserName(QString username)
{
userName = username;
ui->label_userName->setText(username);
}
void homeWindow::start()
{
// Show the login window
loginWindow->setFixedSize(400, 400);
loginWindow->show();
}
void homeWindow::on_pushButton_learn_clicked()
{
// When learn is clicked, start a new session
trainerW->show();
trainerW->startSession();
}
void getTrainerData() {
//qDebug() << QString::number(trainerW->getIncorrectScore());
//qDebug() << QString::number(trainerW->getCorrectScore());
}
#ifndef LOGIN_H
#define LOGIN_H
#include <QDialog>
//#include <homewindow.h>
//#include <session_adjust.h>
namespace Ui {
class login;
}
class login : public QDialog
{
Q_OBJECT
public:
explicit login(QWidget *parent = 0);
~login();
signals:
void loginRequest(bool);
private slots:
void on_pushButton_login_clicked();
void on_pushButton_quit_clicked();
private:
Ui::login *ui;
//session_adjust *sessionW;
//homeWindow *hWindow;
};
#endif // LOGIN_H
#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QStatusBar>
login::login(QWidget *parent) :
QDialog(parent),
ui(new Ui::login)
{
ui->setupUi(this);
ui->label_messegeBar->setText("");
}
login::~login()
{
delete ui;
}
void login::on_pushButton_login_clicked()
{
QString username = ui->lineEdit_username->text();
QString password = ui->lineEdit_password->text();
if(username == "Test" && password == "test"){
/*hWindow = new homeWindow;
hWindow->show();
hWindow->setUserName(username);
login::hide();*/
/*
sessionW = new session_adjust;
sessionW->show();
login::hide();
*/
emit loginRequest(true);
}else{
//QMessageBox::warning(this, "Login", "Wrong username or password.");
ui->label_messegeBar->setText("Username or password is wrong.");
emit loginRequest(false);
}
}
void login::on_pushButton_quit_clicked()
{
QApplication::quit();
}
答案 0 :(得分:2)
您的Container
---------
ID | Number | Title | Date | Colour | Height | Length |
====================================================================================
1 | 10000 | 1st Title | 1/12/2017 | Blue | 5000 | 9100 |
2 | 10543 | 2nd Title | 1/31/2017 | | | |
3 | 10667 | 3rd Title | 4/12/2016 | | | |
4 | 12889 | 4th Title | 5/23/2012 | Gray | | |
声明不正确;你不能把参数值放在那里,只有类型。因此,连接无法创建,因此在发出信号时没有任何反应。在程序的输出中,您将看到错误。你不能这样做:
For Each customField In _db.ContainerCustomFields
.Where(Function(cf) cf.ContainerID = newBox.ContainerID)
returnContainers.Select(Function(c) New With
{.ContainerID = c.ContainerID,
.Number = c.ContainerNumber,
.Title = c.Title,
.Date = c.Date })
因为connect
的参数没有值。您可以做的是更改connect(loginWindow, SIGNAL(loginRequest()), this, SLOT(setVisible(bool)));
信号以获取参数:
setVisible
然后loginRequest
语句将是:
void loginRequest (bool visible);
而connect
将是:
connect(loginWindow, SIGNAL(loginRequest(bool)), this, SLOT(setVisible(bool)));
如果您使用的是Qt 5或更高版本,emit
语句的更好语法是:
emit loginRequest (true);
使用这种新机制,会在编译时而不是运行时捕获问题。
答案 1 :(得分:0)
为登录尝试新的UI类:而不是QDialog(对话框)选择QWidget(小部件)。