PDA

View Full Version : Cutom signal on mouse click at top left corner of QTableWidget



binaural
29th May 2010, 21:47
Hi,

I need to add some functionality (change header items texts) after user click on top left corner of qtablewidget. I try to use signal itemSelectionChanged() but it works only first time because selection is not changed until user click on some other table cell. Simply I need after every click to top left corner cell switch header text from decimal to hexadecimal or whatever. How can I use some mouse event or signal to have such a functionality? Thanks in advance

alexisdm
29th May 2010, 22:11
If by top left corner, you mean the first column header, you can get the horizontal header from QTableView::horizontalHeader, connect its sectionClicked(int) (http://doc.trolltech.com/latest/qheaderview.html#sectionClicked) signal to your custom slot and test the column index given by the signal parameter.

binaural
29th May 2010, 22:31
Hi,

thanks for hint but signal is emmited after click on header item 0 and so on. But I need signal emited when click let's say -1 column( top left).

My code:


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->tableWidget->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(my_slot(int)));
}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::my_slot(int index)
{
qDebug() << index;
}

alexisdm
29th May 2010, 23:30
You meant the "corner button".

You can either try to redefine the selectAll() method by deriving QTableWidget, because this is the virtual slot that is called when you click on it. You should probably test that the sender() is a QAbstractButton and has the QTableWidget as parent.

Or get the button, disconnect any slot, and reconnect your own:

QAbstractButton *cornerButton = tableWidget->findChild<QAbstractButton*>();
// Since it's not part of the API, there is no guarantee it exists
if (cornerButton)
{
cornerButton->disconnect();
connect(cornerButton, SIGNAL(clicked()), ..., ...);
}

binaural
30th May 2010, 13:55
Thanks for advice. I choose solution 1. I post it here so somebody else could use it :).
mainwindow.h


class my_table : public QTableWidget
{
Q_OBJECT

public:
my_table(int row, int col, QWidget *parent = 0);
~my_table();

private:


private slots:
void selectAll ();
};


mainwindow.cpp


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

my_table *table = new my_table(2,2,this);
table->setGeometry(10,20,250,190);


}

MainWindow::~MainWindow()
{
delete ui;
}

my_table::my_table(int row, int col, QWidget *parent)
: QTableWidget(row, col, parent)
{

}

my_table::~my_table()
{

}

void my_table::selectAll(void)
{
qDebug() << "select";
}



Thanks