Читать книгу 📗 "Основы программирования в Linux - Мэтью Нейл"
Q_OBJECTpublic: LogonDialog(QWidget* parent = 0, const char *name = 0); QString getUsername();QString getPassword();private: QLineEdit *username_entry, *password_entry;};2. У вас есть более удобные методы для имени пользователя и пароля, чем инкапсуляция в файле LogonDialog.cpp вызова
database_start#include "LogonDialog.h"#include "appmysql.h"#include <qpushbutton.h>#include <qlayout.h>#include <qlabel.h>LogonDialog::LogonDialog(QWidget *parent, const char *name): QDialog(parent, name) { QGridLayout *grid = new QGridLayout(this, 3, 2, 10, 5, "grid"); grid->addWidget(new QLabel("Username", this, "usernamelabel"), 0, 0, 0); grid->addWidget(new QLabel("Password", this, "passwordlabel"), 1, 0, 0); username_entry = new QLineEdit(this, "username entry"); password_entry = new QLineEdit(this, "password_entry"); password_entry->setEchoMode(QLineEdit::Password); grid->addWidget(username_entry, 0, 1, 0); grid->addWidget(passwordentry, 1, 1, 0); QPushButton* button = new QPushButton("Ok", this, "button"); grid->addWidget(button, 2, 1, Qt::AlignRight); connect (button, SIGNAL(clicked()), this, SLOT(accept()));}QString LogonDialog::getUsername() { if (username_entry == NULL) return NULL; return username_entry->text();}QString LogonDialog::getPassword() { if (password_entry == NULL) return NULL; return password_entry->text();}На рис. 17.15 показано, как будет выглядеть диалоговое окно.

Рис. 17.15
main.cpp
Единственный оставшийся программный код — функция
main1. В файле main.cpp вы открываете окно
LogonDialogdatabase_startQMessageBoxLogonDialog#include "MainWindow.h"#include "app_mysql.h"#include "LogonDialog.h"#include <kde/kapp.h>#include <qmessagebox.h>int main(int argc, char **argv) { char username[100]; char password[100]; KApplication a(argc, argv, "cdapp"); LogonDialog *dialog = new LogonDialog(); while (1) { if (dialog->exec() == QDialog::Accepted) { strcpy(username, dialog->getUsername()); strcpy(password, dialog->getPassword()); if (database_start(username, password)) break; QMessageBox::information(0, "Title", "Could not Logon: Check username and/or password", QMessageBox::Ok); continue; } else { if (QMessageBox:information(0, "Title", "Are you sure you want to quit?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) { return 0; } } } delete dialog; MainWindow *window = new MainWindow("Cd App"); window->resize(600, 400); a.setMainWidget(window); window->show(); return a.exec();}2. Осталось только написать pro-файл для утилиты
qmakeTARGET = appMOC_DIR = mocOBJECTS_DIR = objINCLUDEPATH = /usr/include/kde /usr/include/mysqlQMAKE_LIBDIR_X11 += -/usr/libQMAKE_LIBDIR_X11 += /usr/lib/mysqlQMAKE_LIBS_X11 += -lkdeui -lkdecore -lmysqlclientSOURCES = MainWindow.cpp main.cpp app_mysql.cpp AddCdDialog.cpp LogonDialog.cppHEADERS = MainWindow.h app_mysql.h AddCdDialog.h LogonDialog.h