我為資料庫中的資料動態創建了按鈕
QPushButton *btnComment = new QPushButton("Comment");
btnComment->setProperty("id",qry.value(0).toString());
我動態創建的按鈕是我設定的連接嗎
connect(btnComment, &QPushButton::clicked, this, &Planner::commentButton);
并在插槽上創建了一個函式
public slots:
void commentButton();
如何獲取 id 值以便在單擊按鈕后執行 SQL 查詢
我測驗了功能
void Planner::commentButton()
{
QMessageBox inpass;
inpass.setText("Comment");
inpass.exec();
return;
}
它可以作業,但單擊“確定”后應用程式關閉
我在控制臺中得到類似的東西
QMainWindowLayout::addItem: Please use the public QMainWindow API instead
任何可能的方法?
更新
我能夠通過將傳遞變數宣告為全域變數來解決 lambda 問題
connect(btnComment, &QPushButton::clicked, this, [this]{ commentButton(taskID); });
uj5u.com熱心網友回復:
正如評論中提到的,您可以connect使用 lambda 而不是直接使用非靜態成員。
將簽名/定義更改Planner::commentButton為...
void Planner::commentButton (QPushButton *button)
{
/*
* Use button accordingly.
*/
}
然后只需將您的connect呼叫更改為...
connect(btnComment, &QPushButton::clicked, this,
[this, btnComment]
{
commentButton(btnComment);
});
現在一個指向QPushButton觸發呼叫的指標將被傳遞給Planner::commentButton.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/409021.html
標籤:
