如何访问这个变量Qt?

时间:2018-04-02 05:49:01

标签: c++ qt qt5 qdialog

对不起,这个问题会很长。我不知道如何缩短这一点。我是Qt的新手。有了一点c ++的经验,我想我应该从Qt开始,使我的程序图形化。我选择了一个用户库管理软件作为个人项目。

我做了一本书课,以获得并设定书的名称,作者和uid。然后,我创建了一个库类来管理书籍矢量。它可以添加目前只添加一本书并在特定索引处获得一本书。

所以,到目前为止,代码工作正常。但我接着尝试添加GUI。

在mainwindow构造函数中,我刚刚向库类对象添加了两个预定义的书籍。主窗口有3个Line-edits,用于显示每本书的名称,作者和uid。它有两个按钮" next"显示下一本书"之前"显示上一本书。

我想要一个功能来添加这本书。因此,创建了文件菜单并使用设计菜单添加了添加书。我去了插槽。

我想要的是创建第二个窗口来询问新书的名称,作者和uid。事实上我的库类对象包含所有书籍的详细信息。如何访问该对象以调用addBook()函数来添加该书。

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "library.h"
#include "dialog.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    library getLib();

private slots:
    void on_next_clicked();

    void on_previous_clicked();

    void on_actionAdd_book_triggered();

private:
    Ui::MainWindow *ui;
    int currentIndex;
    library l;

    Dialog* d;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    book b;
    b.setAuthor("Ruskin Bond");
    b.setName("The Jungle Book");
    b.setUid("123456789");

    l.addBook(b);

    b.setAuthor("Savi Sharma");
    b.setName("This is not your story");
    b.setUid("789456123");

    l.addBook(b);

    b = l.getBook(0);
    ui->lineEdit->setText(QString::fromStdString(b.getName()));
    ui->lineEdit_2->setText(QString::fromStdString (b.getAuthor()) );
    ui->lineEdit_3->setText(QString::fromStdString(b.getUid()));

    currentIndex = 0;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_next_clicked()
{
    ++currentIndex;

    if(currentIndex < l.numOfBooks())
    {
        book b;

        b = l.getBook(currentIndex);

        ui->lineEdit->setText(QString::fromStdString(b.getName()));
        ui->lineEdit_2->setText(QString::fromStdString(b.getAuthor()));
        ui->lineEdit_3->setText(QString::fromStdString(b.getUid()));
    }
}

void MainWindow::on_previous_clicked()
{
    --currentIndex;

    if(currentIndex >= 0)
    {
        book b;

        b = l.getBook(currentIndex);

        ui->lineEdit->setText(QString::fromStdString(b.getName()));
        ui->lineEdit_2->setText(QString::fromStdString(b.getAuthor()));
        ui->lineEdit_3->setText(QString::fromStdString(b.getUid()));
    }
}

void MainWindow::on_actionAdd_book_triggered()
{
    d = new Dialog(this);

    d->show();
}

Dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

private slots:
    void on_pushButton_clicked();

private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked()
{
    QString temp = ui->lineEdit->text(),
            temp_2 = ui->lineEdit_2->text(),
            temp_3 = ui->lineEdit_3->text();

    if(temp == "" || temp_2 == "" || temp_3 == "")
        QMessageBox :: warning(this, "Warning!", "One of the lines is empty");
    else
    {
        book b(temp.toStdString(), temp_2.toStdString(), temp_3.toStdString());

        //how do i add the book?
    }
}

这就是出现的情况: Android SDK Platforms Screenshot

2 个答案:

答案 0 :(得分:1)

我希望addBook in library类是将这本书添加到列表中。您可以创建一个连接,将从Dialog到Mainwindow传递书籍的详细信息: -

在Dialog类中创建一个信号,该信号将发送书籍的详细信息。例如在Dialog.h类中的Dialog声明信号如下: -

signals:
     void bookDetailsEntered(book b);

在Dialog.cpp中,在on_pushButton_clicked()中发出此信号: -

void Dialog::on_pushButton_clicked()
{
    QString temp = ui->lineEdit->text(),
           temp_2 = ui->lineEdit_2->text(),
           temp_3 = ui->lineEdit_3->text();

    if(temp == "" || temp_2 == "" || temp_3 == "")
       QMessageBox :: warning(this, "Warning!", "One of the lines is 
       empty");
   else
   {
       book b(temp.toStdString(), temp_2.toStdString(), temp_3.toStdString());

       emit bookDetailsEntered(b);
  }
}

现在在MainWindow.h中声明一个插槽,它将收到这本书的详细信息: -

private slots:
     void onBookDetailsEntered(book b);

并在MainWindow.cpp中创建从Dialog.h中的信号到Mainwindow中的插槽的连接,如下所示: -

void MainWindow::on_actionAdd_book_triggered()
{
    d = new Dialog(this);

    connect(d,SIGNAL(bookDetailsEntered(book)),
           this,SLOT(onBookDetailsEntered(book)));

    // FYI, You can use Dialog here like Dialog d(in stack instead of heap). 
    // By this the d variable will get destroyed once the d is out of scope. 
    // Here you're creating the multiple instance of Dialog(each time when you show Dialog which will consume more memory)
}

void MainWindow::onBookDetailsEntered(book b)
{
   library.addBook(b);
}

我希望我回答你的问题。

答案 1 :(得分:0)

您需要在on_actionAdd_book_triggered()中显示该对话框。即 D-&gt;显示();