2013-05-31 54 views

回答

0

您需要創建一個新類,例如MyWidget,從QWidget或QDialog派生,您想要它,在其中創建插槽 - setText(QString txt)或者您喜歡。在MyWidget

顯示在標籤文本,你的插槽setText(QString txt)應該是這樣的:

MyWidget::setText(QString txt){ 
    myLabel->setText(txt); // myLabel is Qlabel, that you created in MyWidget class  
} 

... code that leads to moment, where you want to create another widget 
MyWidget* wg=new MyWidget(); 
wg->setText("And that's all the magic"); // you pass text to that widget's slot setText(), where you use it however you like 

編輯 - 然後在你的主窗口類,建立MyWidget instange

+0

那我該如何在標籤中顯示它? –

+0

編輯後爲你 – Shf

+0

我試過你說了什麼,但我仍然有這個錯誤:(有什麼事嗎? 'class MyWidget'沒有名爲'setText'的成員 –

1

我不太確定你在這裏問什麼,因爲我覺得這很簡單,但這裏是完整的代碼頭文件:

#ifndef CMAINWINDOW_H 
#define CMAINWINDOW_H 

#include <QDialog> 
#include <QLabel> 
#include <QPushButton> 

/* this is your dialog with results. 
    It contains one label to show the value of parameter passed in. */ 
class CResults : public QDialog 
{ 
    Q_OBJECT 

    public: 
    CResults(const QString & text = QString(), QWidget *parent = 0); 

    void setText(const QString & text); 

    private: 
    QLabel *m_lbl; 
}; 

/* This is your main top-level window which contains two push buttons. 
    Each of them triggers one dialog and passes a different parameter to it. */ 
class CMainWindow : public QWidget 
{ 
    Q_OBJECT 

    public: 
    CMainWindow(void); 


    private slots: 
    void onDialog1BtnClick(void); 
    void onDialog2BtnClick(void); 

    private: 
    QPushButton *m_pb_dlg1; 
    QPushButton *m_pb_dlg2; 
    CResults *m_dlg1; 
    CResults *m_dlg2; 
}; 

#endif // CMAINWINDOW_H 

,這裏是實現:

#include "CMainWindow.h" 

#include <QVBoxLayout> 

CResults::CResults(const QString & text, QWidget *parent) 
    : QDialog(parent) 
{ 
    m_lbl = new QLabel(text, this); 
} 

void CResults::setText(const QString & text) 
{ 
    m_lbl->setText(text); 
} 


CMainWindow::CMainWindow(void) 
: QWidget(0) 
{ 
    /* The following line shows one way of passing parameters to widgets and 
    that is via constructor during instantiation */ 
    m_dlg1 = new CResults("parameter", this); 
    m_dlg2 = new CResults(QString(), this); 
    m_pb_dlg1 = new QPushButton("Dialog1", this); 
    connect(m_pb_dlg1, SIGNAL(clicked()), SLOT(onDialog1BtnClick())); 
    m_pb_dlg2 = new QPushButton("Dialog2", this); 
    connect(m_pb_dlg2, SIGNAL(clicked()), SLOT(onDialog2BtnClick())); 

    QVBoxLayout *l = new QVBoxLayout(this); 
    l->addWidget(m_pb_dlg1); 
    l->addWidget(m_pb_dlg2); 

    setLayout(l); 
} 

void CMainWindow::onDialog1BtnClick(void) 
{ 
    m_dlg1->exec(); 
} 

void CMainWindow::onDialog2BtnClick(void) 
{ 
    /* In this case you want to override the default value passed to constructor, 
    so you will use the setter function */ 
    m_dlg2->setText("Something random"); 
    m_dlg2->exec(); 
} 

如果您的意思是別的,請更具體,以便我可以調整我的答案。

+0

感謝您的回答,正如我所說我在qt上如此新穎,我希望如果你能評論上面的代碼,所以我會知道每個添加的代碼(你添加它)的含義。我會很感激你的幫助,因爲我很痛苦,我沒有時間。我的意思是我的問題是有一個簡單的例子,在主窗口中傳遞一個字符串參數讓我們說這是從文本編輯,我希望它顯示在第二個表單標籤..可以幫助我嗎? –

+0

@NermeenoAlami @NermeenoAlami對於評論感到抱歉,基本上我做的是我爲Results對話框創建了一個setter函數,並在顯示對話框之前調用此函數來設置對話框的參數。我承認這個問題的答案是相當複雜的,但我希望代碼能夠在框外編譯,這樣你就可以對它進行實驗(你只需要在main()中實例化'CMainWindow'類, '就是這樣)。 雖然Shf的反應可能會更好理解。 – jcxz