[英]Qt signals and slots passing data
我對c++ and qt
很c++ and qt
。 我不確定我是否使用正確的術語來描述我想要實現的目標。 但是在這里。
當用戶pushes buttons
時,我的應用程序會spawns and removes widgets in a gridlayout
。 成功地做到了這一點。 但是,當用戶使用生成的窗口小部件時,我希望這些widgets to interact with each other
。
QList<QLineEdit*> m_ptrLEPathList;
QList<QPushButton*> m_ptrPBList;
qint8 m_noFields;
void MainWindow::on_pbIncFields_clicked()
{
//create widgets and place on a new row in a gridLayout
QLineEdit *lineEditPath = new QLineEdit(this);
QPushButton *pushButton = new QPushButton(this);
//storing pointers in lists to be able to delete them later.
m_ptrLEPathList.append(lineEditPath);
m_ptrPBList.append(pushButton);
ui->gridLayout->addWidget(m_ptrLEPathList.last(),m_noFields,0);
ui->gridLayout->addWidget(m_ptrPBList.last(),m_noFields,1);
connect(m_ptrPBList.last(), SIGNAL(clicked(bool), this, SLOT(on_addPath()));
m_noFields++;
}
void MainWindow::on_pbDecFields()
{
//delete last spawned widgets
}
void MainWindow::on_addPath()
{
QFileDialog getPath();
getPath.exec();
//somehow set the text of the line edit spawned on the same row as the pushbutton
}
因此,當我按下任何衍生按鈕時,都會執行我的廣告位,但是我不知道如何store the data from the file dialog in the related lineEdit
。
是我要嘗試做的基本想法,還是有其他解決方案可以實現我想要的功能?
在on_addPath
插槽中,您可以使用QObject::sender
方法獲取單擊的按鈕,並且假設m_ptrLEPathList
和m_ptrPBList
列表相等,則可以輕松獲取相應的QLineEdit
:
void MainWindow::on_addPath()
{
QFileDialog dialog;
if (!dialog.exec())
{
return;
}
QStringList fileNames = dialog.selectedFiles();
if (fileNames.isEmpty())
{
return;
}
QPushButton *btn = qobject_cast<QPushButton*>(sender());
if (!btn)
{
return;
}
Q_ASSERT(m_ptrPBList.size() == m_ptrLEPathList.size());
int index = m_ptrPBList.indexOf(btn);
if (index == -1)
{
return;
}
QLineEdit *edit = m_ptrLEPathList.at(index);
edit->setText(fileNames.first());
}
將地圖添加到私人部分
QMap<QPushButton*, QLineEdit*> map;
然后
QLineEdit *lineEditPath = new QLineEdit(this);
QPushButton *pushButton = new QPushButton(this);
map.insert(pushButton, lineEditPath);
您可以使用sender()方法,如下所示:
void on_addPath()
{
QFileDialog getPath();
getPath.exec();
QObject* obj = sender();
QPushButton *pb = 0;
if((pb = qobject_cast<QPushButton *>(obj)) != 0) {
QLineEdit* lineEdit = map->value(pb, 0);
if( lineEdit != 0 )
lineEdit->setText( getPath.<some function to get selected file name> );
}
}
您將'on_addPath'函數包括在'MainWindow'類的范圍之外,因此,調用插槽時,您將無法訪問該類中的成員元素。
嘗試將slot函數包括在類中,並檢查您是否可以直接訪問成員元素。 另外,“ lineEditPath”元素必須是成員對象,因此必須將其包含在類定義中。
像這樣:
void MainWindow::on_addPath()
{
QFileDialog getPath();
getPath.exec();
QStringList fileNames = dialog.selectedFiles();
if (fileNames.isEmpty())
{
return;
}
m_lineEditPath->setText(fileNames.first());
}
首先, void on_addPath()
必須為void MainWindow::on_addPath()
至於從QFileDialog
鏈接數據,這很簡單。 嘗試這個:
void MainWindow::on_addPath() {
/* Get the push button you clicked */
QPushButon *btn = qobject_cast<QPushButton*>( sender() );
/* Make sure both the lists have the same size */
Q_ASSERT(m_ptrPBList.size() == m_ptrLEPathList.size());
/* If the sender is a button in your list */
if ( m_ptrPBList.contains( btn ) ) {
/* Get the index of your push button */
int idx = m_ptrPBList.indexOf( btn );
/* Get the corresponding line edit */
QLineEdit *le = m_ptrLEPathList.at( idx );
/* Get your path */
QString path = QFileDialog::getOpenFileName( this, "Caption", "Default Location" );
/* If you path is not empty, store it */
if ( not path.isEmpty() )
le->setText( path );
}
}
我認為,最適合的解決方案是在適合您的項目的自定義小部件類中包含QLineEdit
和QPushButton
。 這樣,您可以在此類中使用文件對話框,而無需將小部件存儲在列表中。 很難為您提供所有信息,因為您並未真正提供應用程序應該做什么的任何細節。 但是無論如何,自定義窗口小部件類的外觀如下(您應該在.cpp
文件中定義所有函數):
#ifndef WIDGETCONTAINER_H
#define WIDGETCONTAINER_H
#include <QWidget>
#include <QLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QFileDialog>
class WidgetContainer : public QWidget
{
Q_OBJECT
public:
WidgetContainer(QWidget *parent = 0) : QWidget(parent)
{
setLayout(new QHBoxLayout);
button.setText("BUTTON");
layout()->addWidget(&lineEdit);
layout()->addWidget(&button);
connect(&button, SIGNAL(clicked()), this, SLOT(buttonPressed()));
}
private:
QLineEdit lineEdit;
QPushButton button;
private slots:
void buttonPressed()
{
QString filename = QFileDialog::getSaveFileName();
lineEdit.setText(filename);
}
};
#endif // WIDGETCONTAINER_H
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.