0

I need to handle columns widths resizing in QTableWidget.

How can I do this?

--

I talk about event when cursor is under border between to header sections, left mouse button is down and mouse is moving.

Jablonski
  • 18,083
  • 2
  • 46
  • 47
Ufx
  • 2,595
  • 12
  • 44
  • 83

1 Answers1

2

Try to connect ui->tableWidget->verticalHeader() (it returns QHeaderView) sectionResized() signal to some slot.

Working examples:

New signal and slots syntax + lambda expressions

connect(ui->tableWidget->horizontalHeader(),&QHeaderView::sectionResized,[=]( int logicalIndex, int oldSize, int newSize) {//with lambda
    qDebug() << "works" << logicalIndex << oldSize << newSize;
});

Output:

works 0 115 116 
works 0 116 115 
works 1 100 101 
works 1 101 102 

Also add CONFIG += c++11 to the pro file.

Example with old syntax:

In header:

private slots:
   void clicked(int, int, int);

In constructor:

connect(ui->tableWidget->horizontalHeader(),SIGNAL(sectionResized(int,int,int)),this, SLOT(clicked(int,int,int)));

Slot:

void MainWindow::clicked(int logicalIndex, int oldSize, int newSize)
{
    qDebug() << "works" << logicalIndex << oldSize << newSize;
}

Output:

works 0 106 107 
works 0 107 108 
works 1 100 101 
works 1 101 102 
works 1 102 103 

Choose the best for you, but note that new syntax has:

  • Compile time check of the existence of the signals and slot, of the types, or if the Q_OBJECT is missing.
  • Argument can be by typedefs or with different namespace specifier, and it works.
  • Possibility to automatically cast the types if there is implicit conversion (e.g. from QString to QVariant)
  • It is possible to connect to any member function of QObject, not only slots.

See more information: http://qt-project.org/wiki/New_Signal_Slot_Syntax

Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • @Ufx I added more useful information, you should use another signal, but I tested it and it works now. I added very detail explanation. – Jablonski Oct 12 '14 at 14:45