如何将一个类的变量成员的值赋予另一个类

时间:2015-04-03 11:05:18

标签: c++ qt

一个类有一个组合框,用户必须从中选择一个数字。然后将该值分配给变量,并且由于组合框项目是文本,保存所选用户的数字(换句话说,文本)的变量将使用' toInt()'转换为整数值。并分配给同一个类中的整数变量。现在,不同的类必须能够访问其他类的整数变量(即用户选择的数字),以便根据它的值,向用户显示相同数量的小部件。当我从组合框中选择数字并按下按钮时,不会显示任何内容。我期望从组合框中选择相同的数字到dsiplay作为行编辑。

代码;

combobox.h

#ifndef COMBOBOX_H
#define COMBOBOX_H

#include <QDialog>
#include <QComboBox>
#include <QPushButton>
#include <QStringList>
#include <QVBoxLayout>

class ComboBox : public QDialog {
public:
    ComboBox();
    ~ComboBox();

private slots:
    void on_go_button_clicked();

private:
    QComboBox *comboBox;

public:
    QPushButton *go;
    int textToInt;
};

#endif // COMBOBOX_H

lineeditwidgets.h

#ifndef LINEEDITWIDGETS_H
#define LINEEDITWIDGETS_H

#include <QDialog>
#include <QLineEdit>
#include <QVBoxLayout>
#include "combobox.h"

class LineEditWidgets : public QDialog {
public:
    LineEditWidgets();
    LineEditWidgets(ComboBox*);      //edited
    ~LineEditWidgets();

private:
    QLineEdit *lineEdit;

    //object created so member of class 'ComboBox' can be accessed
    ComboBox *take;
};

#endif // LINEEDITWIDGETS_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>

#include "lineeditwidgets.h"
#include "combobox.h"

class MainWindow : public QMainWindow {
    Q_OBJECT

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

private slots:
    void go_button_clicked();

private:
    QStackedWidget *pages;

    ComboBox *comboBoxWindow;
    LineEditWidgets *lineEditWindow;

};

#endif // MAINWINDOW_H

combobox.cpp

#include <QtGui>
#include "combobox.h"

ComboBox::ComboBox() {
    comboBox = new QComboBox;
    QStringList items;
    items << "1" << "2" << "3" << "4" << "5";
    comboBox->addItems(items);

    go = new QPushButton("Go");

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(comboBox);
    layout->addWidget(go);

    setLayout(layout);

    /*i get 'No such slot QDialog::on_go_button_clicked()' even though this 
      function was declared as a slot in the header file*/
    connect(go, SIGNAL(clicked()), this, SLOT(on_go_button_clicked()));
}

/*the number selected from combobox is converted to integer here. I can't 
  seem to understand how 'toInt()' works, i thought it was like 
  'stringstream()' */
void ComboBox::on_go_button_clicked() {
    QString text;
    comboBox->currentTextChanged(text);
    QString getCurrentText = comboBox->currentText();
    textToInt = getCurrentText.toInt();
}

ComboBox::~ComboBox() {

}

lineeditwidgets.cpp

#include <QtGui>
#include "lineeditwidgets.h"

LineEditWidgets::LineEditWidgets(ComboBox* comboBoxWin) {  //edited
    /*member of 'ComboBox' class is accessed here and assigned to int 
      variable. I thought it's supposed to be holding the number the user 
      selected by now*/        
    /*take = new ComboBox;*/           //edited
      take = comboBoxWin;              //edited

    int numb = take->textToInt;

    QVBoxLayout *layout = new QVBoxLayout;

    for (int i = 0; i < numb; i++) {
        lineEdit = new QLineEdit;
        layout->addWidget(lineEdit);
    }

    setLayout(layout);
}

LineEditWidgets::~LineEditWidgets(){

}

mainwindow.cpp

#include "mainwindow.h"
#include <ui_mainwindow.h>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){
    comboBoxWindow = new ComboBox;
    /*lineEditWindow = new LineEditWidgets;*/     //edited
     lineEditWindow = new LineEditWidgets(comboBoxWindow);  //edited

    pages = new QStackedWidget;
    pages->insertWidget(1, comboBoxWindow);
    pages->insertWidget(2, lineEditWindow);
    pages->setCurrentWidget(comboBoxWindow);

    setCentralWidget(pages);

    connect(comboBoxWindow->go, SIGNAL(clicked()), this,  
    SLOT(go_button_clicked()));
}

void MainWindow::go_button_clicked() {
    pages->setCurrentWidget(lineEditWindow);
}

MainWindow::~MainWindow() {

}

的main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    MainWindow w;
    w.show();

    return a.exec();
}

1 个答案:

答案 0 :(得分:0)

我认为问题在于ComboBox有两个独立的实例。一个在MainWindow中创建 - comboBoxWindow = new ComboBox;,另一个在LineEditWidgets中创建 - take = new ComboBox;

您应该考虑将ComboBox实例 - comboBoxWindow传递给lineEditWindow的构造函数中的LineEditWidgets实例MainWindow,并且lineEditWindow必须保存传递的{{1}实例} ComboBox指针 - ComboBox