mule
26th January 2012, 20:08
I have a Qt Application, class name Secretary, base class QMainWindow.
int main( int argc,
char * argv [] )
{
int exitCode;
QApplication app( argc, argv );
Secretary * mainWindow = new Secretary;
mainWindow->move( 0, 0 );
mainWindow->show();
app.connect( & app, SIGNAL( lastWindowClosed() ),
& app, SLOT( quit() ) );
exitCode = app.exec();
return exitCode;
}
class Secretary : public QMainWindow
{
Q_OBJECT
public:
Secretary( QWidget * parent = 0,
Qt::WFlags flags = 0 );
~Secretary();
private:
Ui::SecretaryClass ui;
Secretary_tabMain * tabMain;
private slots:
void on_tableThings_clicked( const QModelIndex & item );
void on_tableThings_sectionClicked( int logicalIndex );
};
Secretary::Secretary( QWidget * parent,
Qt::WFlags flags )
: QMainWindow( parent, flags )
{
ui.setupUi( this );
tabMain = new Secretary_tabMain( & ui, this );
#if 0 //at run time
QObject::connect( ui.tableThings, SIGNAL( sectionClicked( int ) ),
this, SLOT( on_tableThings_sectionClicked( int ) ) );
#endif //at run time
}
Secretary::~Secretary()
{
;
}
void Secretary::on_tableThings_clicked( const QModelIndex & item )
{
QMessageBox::information( 0, "tableThings", "clicked()" );
}
void Secretary::on_tableThings_sectionClicked( int logicalIndex )
{
QMessageBox::information( 0, "tableThings", "sectionClicked()" );
}
On the UI (in Secretary.ui), one of the widgets on the central widget is a QTabWidget, object name tabMain.
class Secretary_tabMain : public QObject
{
Q_OBJECT
public:
Secretary_tabMain( Ui::SecretaryClass * UI,
QObject * parent = 0 );
~Secretary_tabMain();
private:
Ui::SecretaryClass * pUI;
Secretary_tabMainThings * tabMainThings;
};
Secretary_tabMain::Secretary_tabMain( Ui::SecretaryClass * UI,
QObject * parent )
: QObject( parent ),
pUI( UI )
{
tabMainThings = new Secretary_tabMainThings( pUI, this );
}
Secretary_tabMain::~Secretary_tabMain()
{
;
}
The tab widget has several tabs, the tab of interest is a QWidget, object name tabMainThings.
class Secretary_tabMainThings : public QObject
{
Q_OBJECT
public:
Secretary_tabMainThings( Ui::SecretaryClass * UI,
QObject * parent = 0 );
~Secretary_tabMainThings();
private:
Ui::SecretaryClass * pUI;
TableViewModel0 * thingsModel;
};
Secretary_tabMainThings::Secretary_tabMainThings( Ui::SecretaryClass * UI,
QObject * parent )
: QObject( parent ),
pUI( UI )
{
thingsModel = new TableViewModel0;
thingsModel->initView( pUI->tableThings, thingsModel );
}
Secretary_tabMainThings::~Secretary_tabMainThings( )
{
;
}
The tab of interest has many widgets, the widget of interest is a QTableView, object name tableThings.
QAbstractTableModel has been subclassed to create table model TableViewModel0.
class TableViewModel0 : public QAbstractTableModel
{
Q_OBJECT
public :
TableViewModel0( QObject * parent = 0 );
~TableViewModel0();
void initView( QTableView * view,
TableViewModel0 * model );
};
TableViewModel0::TableViewModel0( QObject * parent )
: QAbstractTableModel( parent )
{
;
}
TableViewModel0::~TableViewModel0()
{
;
}
void TableViewModel0::initView( QTableView * view,
TableViewModel0 * model )
{
view->setModel( model );
view->setTabKeyNavigation( false );
view->setSortingEnabled( false );
view->setSelectionMode( QAbstractItemView::SingleSelection );
view->setSelectionBehavior( QAbstractItemView::SelectRows );
view->horizontalHeader()->setClickable( true );
view->horizontalHeader()->setResizeMode( QHeaderView::ResizeToContents );
view->horizontalHeader()->setHighlightSections( false );
view->horizontalHeader()->setSortIndicatorShown( true );
view->verticalHeader()->hide();
view->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
}
At run time, clicking on an item in table tableThings will call slot Secretary:: on_tableThings_clicked() and display "tableThings ... clicked()".
However, AND THIS IS THE PROBLEM, clicking on a header section of table tableThings will NOT call slot Secretary:: on_tableThings_sectionClicked().
sectionClicked() is a signal from QHeaderView.
The default headers for QTableView are used; that is, no (new) widgets are set to be used for the QTableView headers.
*** NOTE ***
My first attempt, which was unsuccessful, relied on automatic connections being made by UIC. I then realized that the problem could possibly be that the specific view does not exist at compile time and, therefore, the automatic connections could not be made at compile time.
So, my second attempt was to make the connection explictly at runtime using the code within the #if in the Secretary class constructor. This, too, was unsuccessful.
I have tried many minor variations on these themes, all without success.
Insight?
Suggestions?
What do I have wrong? or don't understand?
All assistance greatly appreciated.
Thanks,
mule
int main( int argc,
char * argv [] )
{
int exitCode;
QApplication app( argc, argv );
Secretary * mainWindow = new Secretary;
mainWindow->move( 0, 0 );
mainWindow->show();
app.connect( & app, SIGNAL( lastWindowClosed() ),
& app, SLOT( quit() ) );
exitCode = app.exec();
return exitCode;
}
class Secretary : public QMainWindow
{
Q_OBJECT
public:
Secretary( QWidget * parent = 0,
Qt::WFlags flags = 0 );
~Secretary();
private:
Ui::SecretaryClass ui;
Secretary_tabMain * tabMain;
private slots:
void on_tableThings_clicked( const QModelIndex & item );
void on_tableThings_sectionClicked( int logicalIndex );
};
Secretary::Secretary( QWidget * parent,
Qt::WFlags flags )
: QMainWindow( parent, flags )
{
ui.setupUi( this );
tabMain = new Secretary_tabMain( & ui, this );
#if 0 //at run time
QObject::connect( ui.tableThings, SIGNAL( sectionClicked( int ) ),
this, SLOT( on_tableThings_sectionClicked( int ) ) );
#endif //at run time
}
Secretary::~Secretary()
{
;
}
void Secretary::on_tableThings_clicked( const QModelIndex & item )
{
QMessageBox::information( 0, "tableThings", "clicked()" );
}
void Secretary::on_tableThings_sectionClicked( int logicalIndex )
{
QMessageBox::information( 0, "tableThings", "sectionClicked()" );
}
On the UI (in Secretary.ui), one of the widgets on the central widget is a QTabWidget, object name tabMain.
class Secretary_tabMain : public QObject
{
Q_OBJECT
public:
Secretary_tabMain( Ui::SecretaryClass * UI,
QObject * parent = 0 );
~Secretary_tabMain();
private:
Ui::SecretaryClass * pUI;
Secretary_tabMainThings * tabMainThings;
};
Secretary_tabMain::Secretary_tabMain( Ui::SecretaryClass * UI,
QObject * parent )
: QObject( parent ),
pUI( UI )
{
tabMainThings = new Secretary_tabMainThings( pUI, this );
}
Secretary_tabMain::~Secretary_tabMain()
{
;
}
The tab widget has several tabs, the tab of interest is a QWidget, object name tabMainThings.
class Secretary_tabMainThings : public QObject
{
Q_OBJECT
public:
Secretary_tabMainThings( Ui::SecretaryClass * UI,
QObject * parent = 0 );
~Secretary_tabMainThings();
private:
Ui::SecretaryClass * pUI;
TableViewModel0 * thingsModel;
};
Secretary_tabMainThings::Secretary_tabMainThings( Ui::SecretaryClass * UI,
QObject * parent )
: QObject( parent ),
pUI( UI )
{
thingsModel = new TableViewModel0;
thingsModel->initView( pUI->tableThings, thingsModel );
}
Secretary_tabMainThings::~Secretary_tabMainThings( )
{
;
}
The tab of interest has many widgets, the widget of interest is a QTableView, object name tableThings.
QAbstractTableModel has been subclassed to create table model TableViewModel0.
class TableViewModel0 : public QAbstractTableModel
{
Q_OBJECT
public :
TableViewModel0( QObject * parent = 0 );
~TableViewModel0();
void initView( QTableView * view,
TableViewModel0 * model );
};
TableViewModel0::TableViewModel0( QObject * parent )
: QAbstractTableModel( parent )
{
;
}
TableViewModel0::~TableViewModel0()
{
;
}
void TableViewModel0::initView( QTableView * view,
TableViewModel0 * model )
{
view->setModel( model );
view->setTabKeyNavigation( false );
view->setSortingEnabled( false );
view->setSelectionMode( QAbstractItemView::SingleSelection );
view->setSelectionBehavior( QAbstractItemView::SelectRows );
view->horizontalHeader()->setClickable( true );
view->horizontalHeader()->setResizeMode( QHeaderView::ResizeToContents );
view->horizontalHeader()->setHighlightSections( false );
view->horizontalHeader()->setSortIndicatorShown( true );
view->verticalHeader()->hide();
view->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
}
At run time, clicking on an item in table tableThings will call slot Secretary:: on_tableThings_clicked() and display "tableThings ... clicked()".
However, AND THIS IS THE PROBLEM, clicking on a header section of table tableThings will NOT call slot Secretary:: on_tableThings_sectionClicked().
sectionClicked() is a signal from QHeaderView.
The default headers for QTableView are used; that is, no (new) widgets are set to be used for the QTableView headers.
*** NOTE ***
My first attempt, which was unsuccessful, relied on automatic connections being made by UIC. I then realized that the problem could possibly be that the specific view does not exist at compile time and, therefore, the automatic connections could not be made at compile time.
So, my second attempt was to make the connection explictly at runtime using the code within the #if in the Secretary class constructor. This, too, was unsuccessful.
I have tried many minor variations on these themes, all without success.
Insight?
Suggestions?
What do I have wrong? or don't understand?
All assistance greatly appreciated.
Thanks,
mule