2016-03-07 38 views
0

我嘗試了很多事情,並且一整天都沒有看到真正的進展。即使在我輸入這個問題時,我也經歷了所有堆棧溢出建議。 我的目標是?讓dialog2向dialog1發出一個信號來調用dialog1中的函數,以加載/更新QWidgetTable。 在dialog2(no Buttons)中,keyPressEvent(F4)打開一個文件,將文本保存在一個lineEdit中,並保存到文件中。在流出完成之後,流將被刷新,文件關閉並且lineEdit字段被重置,以便您可以根據需要輸入更多內容。所有這些都很完美,部分歸功於這個論壇。 但是,作爲dialog2只不過是一個messageBox的大小,你仍然可以在它後面看到dialog1,而tableWidget不會用你剛剛保存到文件中的東西來更新自己。這對用戶來說看起來很奇怪。 這是我在哪裏...qt dialog2連接dialog1更新QtableWidget

connect(this->ui->lineEdit,SIGNAL(textChanged(QString)),dialog1,SLOT(loadTable())); 

(this)會是dialog2。 它不編譯。我知道我在這裏錯過了一些東西。如果小部件位於自己的「窗體」/「窗口」內,則信號和插槽相對容易。

任何幫助將不勝感激。

Dialog1::Dialog1(QWidget *parent) : 
    QDialog(parent), 
    ui(new Ui::Dialog1) 
{ 
    setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);//Frameless 
    ui->setupUi(this); 
    //Move the dialog away from the center 
    this->setGeometry(0,0,this->width(),this->height()); 
    //Put the dialog in the screen center 
    const QRect screen = QApplication::desktop()->screenGeometry(); 
    this->move(screen.center() - this->rect().center()); 
    loadTable(); 

} 



Dialog2::Dialog2(QWidget *parent) : 
    QDialog(parent), 
    ui(new Ui::Dialog2) 
{ 
    setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);//Frameless 
    ui->setupUi(this); 
    //Move the dialog away from the center 
    this->setGeometry(0,0,this->width(),this->height()); 
    //Put the dialog in the screen center 
    const QRect screen = QApplication::desktop()->screenGeometry(); 
    this->move(screen.center() - this->rect().center()); 
    connect(ui->lineEdit,SIGNAL(textChanged(QString)),&Dialog1,SLOT(loadTable())); 
} 

我不明白。

+0

你說這不編譯 - 你可以讓我們知道什麼樣的錯誤,編譯器給你? – docsteer

+0

如果dialog1不是指針,請在其之前添加&運算符。 connect(this-> ui-> lineEdit,SIGNAL(textChanged(QString)),&dialog1,SLOT(loadTable())); 或 connect(this-> ui-> lineEdit,SIGNAL(textChanged(QString)),ui-> dialog1,SLOT(loadTable())); –

+0

這裏是錯誤:C:\ ... \ ... \ ... \ dialog2.cpp:29:錯誤:預期的主要表達式在第一個建議的','標記之前。 – Hank

回答

0

我相信這個問題是在這裏:

connect(ui->lineEdit,SIGNAL(textChanged(QString)),&Dialog1,SLOT(loadTable())); 

你不必Dialog1實例。如果Dialog1首先出現,那麼您需要將Dialog1實例傳遞給Dialog2,並且可以連接它。

0

我會在你的情況下做這樣的事情。

首先在Dialog2我會創建一個信號。我打算稱這個信號爲dataChanged。保存完成後,我會發出這樣的信號:

dialog2.h

class Dialog2 : public QDialog 
{ 
    Q_OBJECT 
    ... 
signals: 
    void dataChanged(); 
    ... 
}; 

dialog2.cpp:

void Dialog2::save() 
{ 
    // your save functionality... 
    emit dataChanged(); 
} 

現在我不知道你在哪裏創建Dialog1Dialog2類的實例,但如果這兩個實例是在同一個類中創建的,則這將如何連接信號:

class SomeClass : public QWidget 
{ 
    Q_OBJECT 
    ... 
public: 
    void someFunction(); 

private: 
    Dialog1 *dialog1; 
    Dialog2 *dialog2; 
} 

-

void SomeClass::someFunction() 
{ 
    dialog1 = new QDialog(this); 
    dialog2 = new QDialog(this); 

    connect(dialog2, &Dialog2::dataChanged, dialog1, &Dialog1::loadTable); 
}