0

I currently have a QtableView that is attached to a QstandardItemModel. I want to insert a clickable Qlabel in the last column of each row. Initially I wanted to go with a QPushButton but it turns out that it requires extra overhead so now I am planning to go with a clickable Qlabel. Any suggetsions on how I could do that ? Also I would appreciate it if someone could sugget options that are available for simulating click events in a TableView using QStandardItemModel

MistyD
  • 16,373
  • 40
  • 138
  • 240

1 Answers1

0
connect(ui.tableView,SIGNAL(clicked(const QModelIndex& ) ), 
    this,SLOT( itemClicked(const QModelIndex& ) ) );

slot:
    void itemClicked( const QModelIndex& idx) {
      int row = idx.row();
      int column = idx.column();
    }

if you really need just a clickable label you can create class derived from QLabel and add custom signals to handle click event:

class CustomWidget : public QLabel {
Q_OBJECT
public:
explicit CustomWidget(const QString& text, QWidget *parent = 0);

signals:
void released(void);
void clicked(void);

protected:
void mousePressEvent(QMouseEvent* e);
void mouseReleaseEvent(QMouseEvent* e);

private:
bool mousePressed;
};

CustomWidget::CustomWidget(const QString& text, QWidget* parent)
: QLabel(text, parent), mousePressed(false) {
}

void CustomWidget::mousePressEvent(QMouseEvent* e) {
mousePressed = true;
}

void CustomWidget::mouseReleaseEvent(QMouseEvent* e) {
emit released();
if(mousePressed) {
emit clicked();
mousePressed = false;
}
}

full code snippet:

http://www.qtcentre.org/archive/index.php/t-42296.html?s=e9f0fd408147a1cd1048f252967895a0

4pie0
  • 29,204
  • 9
  • 82
  • 118
  • I think this would trigger a click event for anywhere on the cell of the tableview even if the user did not click on the label itself . – MistyD Sep 24 '13 at 19:31
  • then connect label itself – 4pie0 Sep 24 '13 at 19:48
  • Thanks and how can I attach this to a QstandardItem since it needs to be in a StandardItemModel ? – MistyD Sep 24 '13 at 21:07
  • [here](http://www.qtcentre.org/archive/index.php/t-42296.html?s=e9f0fd408147a1cd1048f252967895a0) you can find full code snippet – 4pie0 Sep 24 '13 at 22:55