当我创建一个小部件时,移动对我来说不起作用,我给它前一个窗口(而不是父窗口)的位置,但它仍然不想接受我在 Ubuntu 上的 Qt 中需要的位置,即使
当我创建一个小部件时,移动对我来说不起作用,我给它前一个窗口(而不是父窗口)的位置,但它仍然不想接受我在 Ubuntu 上的 Qt 中需要的位置,尽管在 Windows 上一切正常
代码:
if(query.next()){ // проверка результата запроса
QRect currentGeometry = this->geometry();
QWidget *loggetInWindow = createAccountWindow(userType, currentGeometry);
if(loggetInWindow){
loggetInWindow->show();
loggetInWindow->move(this->x(), this->y());
}
}
else{
QMessageBox::warning(this, "Ошибка входа", "Неверный логин или пароль.");
}
我尝试交换 show 和 move(我不知道哪个才是正确的)但什么都没改变
补充代码,LoginWindow 是程序启动时立即打开的第一个初始窗口,loggetInWindow 这是单击按钮后打开的第二个窗口
#include "ui_loginwindow.h"
#include "registrationwindow.h"
#include "loginwindow.h"
#include "adminwindow.h"
#include "clientwindow.h"
#include "workerwindow.h"
#include <QPropertyAnimation>
#include <QtSql/QSqlDatabase> // для подключения к базе данных
#include <QtSql/QSqlQuery> // для выполения запросов к базе данных
#include <QMessageBox>
LoginWindow::LoginWindow(QWidget *parent)
: QWidget(parent)
, ui(new Ui::LoginWindow)
{
ui->setupUi(this);
QComboBox *comboBox = ui->comboBox;
comboBox->addItem("Клиент");
comboBox->addItem("Администратор");
comboBox->addItem("Работник");
}
LoginWindow::~LoginWindow()
{
delete ui;
}
void LoginWindow::on_pushButton_login_clicked() // кнопка вход
{
QString userType = ui->comboBox->currentText();
QString username = ui->lineEdit_username->text();
QString password = ui->lineEdit_password->text();
{
QSqlDatabase db;
if(QSqlDatabase::contains("loginConnection")){
db = QSqlDatabase::database("loginConnection");
if(!db.open()){
qDebug() << "Failed open database LoginWindow\n";
return;
}
}
else{
db = QSqlDatabase::addDatabase("QMYSQL", "loginConnection");
db.setHostName("localhost");
db.setUserName("zxctatar");
db.setDatabaseName("zxctatar");
if(!db.open()){
qDebug() << "Failed open database LoginWindow\n";
return;
}
}
if(ui->lineEdit_username->text().length() == 0){
QMessageBox::warning(this,"LoginWindow","Введите логин");
return;
}
if(ui->lineEdit_password->text().length() == 0){
QMessageBox::warning(this,"Пароль", "Введите пароль");
return;
}
QSqlQuery query(db);
if(userType == "Клиент"){
query.prepare("SELECT * FROM Клиент WHERE Логин = :login AND Пароль = :password");
}
else if(userType == "Администратор"){
query.prepare("SELECT * FROM Администратор WHERE StrComp(Логин, :login, 0) = 0 AND StrComp(Пароль, :password, 0) = 0");
}
else if(userType == "Работник"){
query.prepare("SELECT * FROM Работник WHERE StrComp(Логин, :login, 0) = 0 AND StrComp(Пароль, :password, 0) = 0");
}
else{
qDebug() << "Unknown user type\n";
db.close();
return;
}
query.bindValue(":login", username);
query.bindValue(":password", password);
if (!query.exec()){
qDebug() << "Failed query\n";
db.close();
return;
}
if(query.next()){ // проверка результата запроса
QRect currentGeometry = this->geometry();
QWidget *loggetInWindow = createAccountWindow(userType, currentGeometry);
if(loggetInWindow){
loggetInWindow->show();
loggetInWindow->move(this->x(), this->y());
}
}
else{
QMessageBox::warning(this, "Ошибка входа", "Неверный логин или пароль.");
}
db.close();
}
QSqlDatabase::removeDatabase("loginConnection");
}
QWidget* LoginWindow::createAccountWindow(const QString& userType, const QRect& geometry){ // проверка какое окно создать
QWidget *loggedInWindow = nullptr;
if(userType == "Клиент"){
loggedInWindow = new ClientWindow(ui->lineEdit_username->text());
loggedInWindow->setAttribute(Qt::WA_DeleteOnClose);
}
else if(userType == "Администратор"){
loggedInWindow = new AdminWindow();
loggedInWindow->setAttribute(Qt::WA_DeleteOnClose);
}
else if(userType == "Работник"){
loggedInWindow = new WorkerWindow(ui->lineEdit_username->text());
loggedInWindow->setAttribute(Qt::WA_DeleteOnClose);
}
if(loggedInWindow){
loggedInWindow->setGeometry(geometry);
loggedInWindow->setWindowOpacity(0.0);
loggedInWindow->show();
}
return loggedInWindow;
}
void LoginWindow::on_pushButton_registration_clicked(){
RegistrationWindow* regWindow = new RegistrationWindow(this);
regWindow->setAttribute(Qt::WA_DeleteOnClose);
regWindow->show();
connect(regWindow, &RegistrationWindow::windowClosed, this, [=](){
delete regWindow;
});
}
最小可重复示例
#include "mainwindow.h"
#include <QScreen>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QScreen *primaryScreen = QGuiApplication::primaryScreen();
QRect availableGeometry = primaryScreen->availableGeometry();
int x = (availableGeometry.width() - w.width()) / 2;
int y = (availableGeometry.height() - w.height()) / 2;
w.move(x,y);
w.show();
return a.exec();
}