我是QT新手,我試圖在兩個表單之間傳遞一個參數,但是我無法做到這一點..有人幫助我,請給我一個從主窗口傳遞字符串常量「參數」到另一個QWidget 0窗口的簡單示例,名爲結果在QT中傳遞兩個QWidget表單之間的參數
回答
您需要創建一個新類,例如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
我不太確定你在這裏問什麼,因爲我覺得這很簡單,但這裏是完整的代碼頭文件:
#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();
}
如果您的意思是別的,請更具體,以便我可以調整我的答案。
感謝您的回答,正如我所說我在qt上如此新穎,我希望如果你能評論上面的代碼,所以我會知道每個添加的代碼(你添加它)的含義。我會很感激你的幫助,因爲我很痛苦,我沒有時間。我的意思是我的問題是有一個簡單的例子,在主窗口中傳遞一個字符串參數讓我們說這是從文本編輯,我希望它顯示在第二個表單標籤..可以幫助我嗎? –
@NermeenoAlami @NermeenoAlami對於評論感到抱歉,基本上我做的是我爲Results對話框創建了一個setter函數,並在顯示對話框之前調用此函數來設置對話框的參數。我承認這個問題的答案是相當複雜的,但我希望代碼能夠在框外編譯,這樣你就可以對它進行實驗(你只需要在main()中實例化'CMainWindow'類, '就是這樣)。 雖然Shf的反應可能會更好理解。 – jcxz
- 1. 如何在Axapta中的兩個表單之間傳遞參數?
- 2. 在qt進程之間傳遞參數?
- 3. 通過兩個表單之間的引用傳遞參數
- 4. 在c#中的兩個表單之間傳遞數據
- 5. Qt,兩個QWidget對象之間的簡單連接
- 6. Qt在多個表單之間傳遞數據
- 7. 如何在兩個jQuery數據表之間傳遞參數
- 8. 在兩頁之間傳遞參數html
- 9. 如何在HTML中的表單之間傳遞參數?
- 10. 在MS Access中傳遞表單之間的參數
- 11. 在兩個打開的表單之間傳遞一個TextBox?
- 12. 在兩個TableView之間傳遞數據
- 13. 在兩個UIViewControllers之間傳遞數據
- 14. Node.JS module.exports用於在兩個函數之間傳遞參數?
- 15. 在表單之間傳遞一個arrayList
- 16. 在表單之間傳遞數據
- 17. 在WPF表單之間傳遞數據
- 18. 在表單之間傳遞數據
- 19. 在VB.NET表單之間傳遞數據
- 20. 在表單之間傳遞數據DIRECTLY
- 21. VB.NET在表單之間傳遞數據
- 22. 在兩個筆尖之間傳遞參數:建議?
- 23. 如何在兩個uiview之間傳遞參數
- 24. 如何在兩個onclick事件之間傳遞參數
- 25. 在兩個控制器之間傳遞參數rails
- 26. 在兩個JSP頁面之間傳遞參數
- 27. 每個函數之間傳遞參數
- 28. 如何在Qt Creator中的兩個.ui表單之間切換?
- 29. 在兩個表單之間傳遞字符串
- 30. 如何在兩種表單之間傳遞文本框數據?
那我該如何在標籤中顯示它? –
編輯後爲你 – Shf
我試過你說了什麼,但我仍然有這個錯誤:(有什麼事嗎? 'class MyWidget'沒有名爲'setText'的成員 –