一個類有一個組合框,用戶必須從中選擇一個數字。然後該值被分配給一個變量,並且由於組合框項目是文本,所以用戶選擇的變量保存數字(換句話說,文本)使用'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();
}
「無法使用它」不是一個有效的問題或描述。有什麼問題?代碼不能編譯?顯示錯誤消息。你會得到不正確的結果?解釋你得到的結果,你期望得到的結果,以及有什麼不同。 – 2015-04-03 11:09:55
@SamVarshavchik什麼都沒有顯示。我已更新我的問題。 – 2015-04-03 11:17:06
取決於訪問說明符,可以使用以下3種方式。 1.如果可以保存爲'public',則可以將用戶輸入的變量保存在全局變量中,並在其他文件中使用'extern'來訪問它。您也可以使用繼承概念來實現這一點。 2.如果它是受保護的,您可以通過派生類訪問它,然後使用上面提到的一些全局變量與外部世界共享它。 3.如果是「私人」數據,則可以通過使用好友功能將其分享給其他類,並從那裏將其分配給全局變量。 – 2015-04-03 11:23:37