2017-10-17 91 views
0

我正在嘗試創建一個用戶登錄頁面,下面是this link。這是我的頭文件formlogin.hQt5 - 嘗試創建登錄頁面時出現「未定義參考」錯誤

#ifndef FORMLOGIN_H 
#define FORMLOGIN_H 

#include <QDialog> 
#include <QLabel> 
#include <QPushButton> 
#include <QDialogButtonBox> 
#include <QLineEdit> 
#include <QComboBox> 
#include <QGridLayout> 
#include <QStringList> 
#include <QDebug> 

/*! 
* Makes class LoginDialog a child to its parent, QDialog 
*/ 
class LoginDialog : public QDialog 
{ 
/*! 
* Turns Login Dialog into a QObject 
*/ 
Q_OBJECT 

private: 
/*! 
* A label for the username component. 
*/ 
QLabel* labelUsername; 

/*! 
* A label for the password. 
*/ 
QLabel* labelPassword; 

/*! 
* An editable combo box for allowing the user 
* to enter his username or select it from a list. 
*/ 
QComboBox* comboUsername; 

/*! 
* A field to let the user enters his password. 
*/ 
QLineEdit* editPassword; 

/*! 
* The standard dialog button box. 
*/ 
QDialogButtonBox* buttons; 

/*! 
* A method to set up all dialog components and 
* initialize them. 
*/ 
void setUpGUI(); 

public: 
explicit LoginDialog(QWidget *parent = 0); 

/*! 
* Sets the proposed username, that can come for instance 
* from a shared setting. 
* username the string that represents the current username 
* to display 
*/ 
void setUsername(QString& username); 

/*! 
* Sets the current password to propose to the user for the login. 
* password the password to fill into the dialog form 
*/ 
void setPassword(QString& password); 

/*! 
* Sets a list of allowed usernames from which the user 
* can pick one if he does not want to directly edit it. 
* usernames a list of usernames 
*/ 
void setUsernamesList(const QStringList& usernames); 

signals: 

/*! 
* A signal emitted when the login is performed. 
* username the username entered in the dialog 
* password the password entered in the dialog 
* index the number of the username selected in the combobox 
*/ 
void acceptLogin(QString& username, QString& password, int& indexNumber); 

public slots: 
/*! 
* A lot to adjust the emitting of the signal. 
*/ 
void slotAcceptLogin(); 

}; 

#endif // LOGINDIALOG_H 

,這是我的cpp文件:

#include "formlogin.h" 

LoginDialog::LoginDialog(QWidget *parent) : 
QDialog(parent) 
{ 
setUpGUI(); 
setWindowTitle(tr("User Login")); 
setModal(true); 
} 

void LoginDialog::setUpGUI(){ 
// set up the layout 
QGridLayout* formGridLayout = new QGridLayout(this); 

// initialize the username combo box so that it is editable 
comboUsername = new QComboBox(this); 
comboUsername->setEditable(true); 
// initialize the password field so that it does not echo 
// characters 
editPassword = new QLineEdit(this); 
editPassword->setEchoMode(QLineEdit::Password); 

// initialize the labels 
labelUsername = new QLabel(this); 
labelPassword = new QLabel(this); 
labelUsername->setText(tr("Username")); 
labelUsername->setBuddy(comboUsername); 
labelPassword->setText(tr("Password")); 
labelPassword->setBuddy(editPassword); 

// initialize buttons 
buttons = new QDialogButtonBox(this); 
buttons->addButton(QDialogButtonBox::Ok); 
buttons->addButton(QDialogButtonBox::Cancel); 
buttons->button(QDialogButtonBox::Ok)->setText(tr("Login")); 
buttons->button(QDialogButtonBox::Cancel)->setText(tr("Abort")); 

// connects slots 
connect(buttons->button(QDialogButtonBox::Cancel), 
SIGNAL (clicked()), 
this, 
SLOT (close()) 
); 

connect(buttons->button(QDialogButtonBox::Ok), 
SIGNAL (clicked()), 
this, 
SLOT (slotAcceptLogin())); 

// place components into the dialog 
formGridLayout->addWidget(labelUsername, 0, 0); 
formGridLayout->addWidget(comboUsername, 0, 1); 
formGridLayout->addWidget(labelPassword, 1, 0); 
formGridLayout->addWidget(editPassword, 1, 1); 
formGridLayout->addWidget(buttons, 2, 0, 1, 2); 

setLayout(formGridLayout); 

} 

void LoginDialog::setUsername(QString &username){ 
bool found = false; 
for(int i = 0; i < comboUsername->count() && ! found ; i++) 
if(comboUsername->itemText(i) == username){ 
comboUsername->setCurrentIndex(i); 
found = true; 
} 

if(! found){ 
int index = comboUsername->count(); 
qDebug() << "Select username " << index; 
comboUsername->addItem(username); 

comboUsername->setCurrentIndex(index); 
} 

// place the focus on the password field 
editPassword->setFocus(); 
} 

void LoginDialog::setPassword(QString &password){ 
editPassword->setText(password); 
} 

void LoginDialog::slotAcceptLogin(){ 
QString username = comboUsername->currentText(); 
QString password = editPassword->text(); 
int index = comboUsername->currentIndex(); 

emit acceptLogin(username, // current username 
password, // current password 
index // index in the username list 
); 

// close this dialog 
close(); 
} 

void LoginDialog::setUsernamesList(const QStringList &usernames){ 
comboUsername->addItems(usernames); 
} 

而且這是我正在嘗試執行登錄頁面:

LoginDialog* loginDialog = new LoginDialog(m_mainWindow); 
connect(loginDialog, SIGNAL(acceptLogin(QString&,QString&,int&)), m_mainWindow, SLOT(slotAcceptUserLogin(QString&,QString&))); 
loginDialog->exec(); 

但每我得到這個錯誤:

undefined reference to `LoginDialog::LoginDialog(QWidget*)' 
collect2: error: ld returned 1 exit status 

我已經嘗試設置

LoginDialog::LoginDialog(QWidget* parent) : 
QDialog(parent) 

要:

LoginDialog::LoginDialog(QWidget* parent=0) : 
QDialog(parent) 

但還是同樣的錯誤。我在網上搜索,看到了這個問題的幾個修復。但沒有人爲我工作。我已經把Q_OBJECT放在我的課堂上了。已經將默認QWidget* parent值設置爲0,但似乎沒有任何工作。

任何想法我應該如何處理這個?

在此先感謝。

+0

什麼是「#include」formlogin.h「」?你的意思是第一個代碼部分? – Alex

+0

@亞歷,是的。第一個代碼部分。 – John

+0

@John不要在cpp中重複默認參數('= 0')。並不是說它不起作用,但它至少有錯誤傾向。 –

回答

0

這看起來像一個鏈接器問題。你有一個makefile來編譯並鏈接它嗎?如果你只是在沒有這個的情況下運行make,它將不知道要鏈接什麼。鏈接器將需要鏈接main.cpp,formlogin.cpp和通過在formlogin.h上運行moc生成的cpp的對象文件 - 正如Froggatt所說的,使用.pro文件是將生成的makefile文件最簡單的方法做所有這一切。

+0

umm,是的,我有一個makefile,我沒有鏈接任何東西。我將在那裏添加對該文件的引用,然後重試。謝謝。 – John

+0

將cpp文件添加到生成文件列表後是的,它工作完美。 – John

相關問題