2016-05-02 8 views
0

私は次のことを達成したいと思います:私はQSqlRelationalTableModelで接続しているMySQLテーブルを持っています。最後の列にデータベースからブール値を取得し、ブール値の値に応じてアクションに接続するビューボタンを表示します。私は(tableModelpublic QSqlRelationalTableModelあるMySqlTableModel、である)モデルとビューを作成しました:Qt - QSqlRelationalDelegateで作成したボタンを接続する方法

tableView = new QTableView(this); 
tableView->setModel(tableModel); 
tableView->setItemDelegate(new tableDelegate(this)); 

私の質問は、私がするTableModelの機能にこれらのボタンを接続するのですか、ですか?

デリゲートは、適切なボタンを生成し、次のようになります。

void tableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { 

    if (index.column() != 4) { 
     QSqlRelationalDelegate::paint(painter, option, index); 
    } else if (index.model()->data(index).toInt() == 1){ 
     QStyleOptionButton button; 
     QRect r = option.rect;//getting the rect of the cell 
     int x,y,w,h; 
     x = r.left() + r.width() - 90;//the X coordinate 
     y = r.top();//the Y coordinate 
     w = 90;//button width 
     h = 30;//button height 
     button.rect = QRect(x,y,w,h); 
     button.text = "Logout"; 
     button.state = QStyle::State_Enabled; 

     QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter); 
    } else { 
     QStyleOptionButton button; 
     QRect r = option.rect;//getting the rect of the cell 
     int x,y,w,h; 
     x = r.left() + r.width() - 90;//the X coordinate 
     y = r.top();//the Y coordinate 
     w = 90;//button width 
     h = 30;//button height 
     button.rect = QRect(x,y,w,h); 
     button.text = "Login"; 
     button.state = QStyle::State_Enabled; 


     QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter); 
    } 
} 

だから、私はそれぞれtableModel.login()またはtableModel.logout()に各ボタンbuttonを接続したいです。どうすればこれを達成できますか?

答えて

0

ボタンはありません。あなたはボタンを描きますが、ボタンは実際には存在しません。セル内に塗られたボタンの「画像」です。あなたは何ができるか

はあなたのスロットにQAbstractItemView::clicked()信号を接続するためのQAbstractItemView::setIndexWidget()メソッドを呼び出したりすることによって、テーブルに本当ボタンを挿入のいずれかです。

2番目の方法を実行する場合は、実際のボタンのようにするには、を押したときに擬似ボタンをQStyle::State_Sunkenで描画する必要があります。

+0

setIndexWidgetメソッドを呼び出すオブジェクトはどれですか? –

+0

それほど重要ではありません。 * tableView *を管理するオブジェクトを考えてみましょう。 – Tomas

+0

'setIndexWidget()'でそれを行うと、私がクリックするとすべてのボタンが消えます。それはそれがどのようになっているのでしょうか? –

関連する問題