PDA

View Full Version : Act when QTableView::isVisible()



i92guboj
27th November 2013, 08:24
Hello.

I am using the isVisible() property to select the currently displayed QTableView, amongst a total of four.

That adjusts a pointer so that I can move a widget across the many tabs where the QTableView live. Right. The problem is that, depending on many factors, the QTableView in a given tabs can be shown or hidden, and, in turn, the widget needs to be enabled or disabled. The code is like this:



void kooker::setCurrentTableView()
{
// first, we move the listEditWidget to the new current tab

QTabWidget *tab = static_cast<QTabWidget*>(ui->tabs->widget(ui->tabs->currentIndex()));
QGridLayout *layout = static_cast<QGridLayout*>(tab->layout());
if((ui->tabs->count() - 1) != ui->tabs->currentIndex())
{
layout->addWidget(ui->listEditWidget, 0, 0);
}

// get the visible table

currentTableView = getVisibleTable();

// if there's one, we enable the widget, else we disable it

if(!currentTableView)
{
ui->listEditWidget->setEnabled(false);
}
else
{
ui->listEditWidget->setEnabled(true);
connect(currentTableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) ,
this, SLOT(checkAddNoteButton()));
}
}

QTableView *kooker::getVisibleTable()
{
if(ui->tableView_cascosYAccesorios->isVisible())
{
return ui->tableView_cascosYAccesorios;
}
else if(ui->tableView_Elec->isVisible())
{
return ui->tableView_Elec;
}
else if(ui->tableView_PuertasYCajones->isVisible())
{
return ui->tableView_PuertasYCajones;
}
else if(ui->tv_herrajes->isVisible())
{
return ui->tv_herrajes;
}
else if(ui->tv_varios->isVisible())
{
return ui->tv_varios;
}
else
{
return NULL;
}
}


Not 100% elegant, but it works (mostly, anyway). In the constructor I have:


connect(ui->tabs, SIGNAL(currentChanged(int)), this, SLOT(setCurrentTableView()));

Which does all the magic when I change he current tab. But, what happens when the program starts? No currentChanged() seems to be automatically emitted when displaying the tabs widget, so, I have to run setCurrentTableView() in the constructor. But that doesn't seem to work either, because currentTableView, according to the debugger, points to 0x0, and, hence, the listEditWidget is not enabled and remains greyed.

So, can you think of any way to either:

wait for ui->tableView*'s to be rendered so that isVisible() on them returns 'TRUE', or
do this in a more elegant way


Thanks for any idea you can share, or just for reading. :)

Santosh Reddy
28th November 2013, 06:40
Don't depend on isVisbile(), instead implement QTreeView::showEvent(), and emit a signal from there and in a connected slot enable/disable the required widgets.

i92guboj
28th November 2013, 09:01
I got it. It worked without a problem, in combination with the reimplementation of ::hideEvent() as well. :)

Thank you.