2015-04-03 42 views
-1

一个类有一个组合框,用户必须从中选择一个数字。然后该值被分配给一个变量,并且由于组合框项目是文本,所以用户选择的变量保存数字(换句话说,文本)使用'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(); 
} 
+3

“无法使用它”不是一个有效的问题或描述。有什么问题?代码不能编译?显示错误消息。你会得到不正确的结果?解释你得到的结果,你期望得到的结果,以及有什么不同。 – 2015-04-03 11:09:55

+0

@SamVarshavchik什么都没有显示。我已更新我的问题。 – 2015-04-03 11:17:06

+0

取决于访问说明符,可以使用以下3种方式。 1.如果可以保存为'public',则可以将用户输入的变量保存在全局变量中,并在其他文件中使用'extern'来访问它。您也可以使用继承概念来实现这一点。 2.如果它是受保护的,您可以通过派生类访问它,然后使用上面提到的一些全局变量与外部世界共享它。 3.如果是“私人”数据,则可以通过使用好友功能将其分享给其他类,并从那里将其分配给全局变量。 – 2015-04-03 11:23:37

回答

0

我认为这个问题是存在的ComboBox两个单独的实例。一个是在主窗口中创建 - comboBoxWindow = new ComboBox;并创建另一个在LineEditWidgets - take = new ComboBox;

你应该考虑通过ComboBox实例 - comboBoxWindow到LineEditWidgets实例lineEditWindowMainWindow构造和lineEditWindow必须的ComboBox传递的实例保存到ComboBox指针 - take

+0

困惑!如果你可以请修复我的代码的一部分,我错了和评论它,所以我明白你的修复,我会感激它 – 2015-04-03 11:38:48

+0

我已经编辑你的代码。 – Vishal 2015-04-03 11:59:59

+0

是的,我已经看到它,但我得到'没有这样的插槽QDialog :: on_go_button_clicked()'警告当我运行代码,使其行为与你的编辑前表现一样。即我没有选择数字后显示行编辑,并按下按钮 – 2015-04-03 12:16:37

相关问题