Results 1 to 6 of 6

Thread: Unable to catch QHeaderView signals

  1. #1
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Unable to catch QHeaderView signals

    I have a Qt Application, class name Secretary, base class QMainWindow.

    Qt Code:
    1. int main( int argc,
    2. char * argv [] )
    3. {
    4. int exitCode;
    5.  
    6. QApplication app( argc, argv );
    7.  
    8. Secretary * mainWindow = new Secretary;
    9. mainWindow->move( 0, 0 );
    10. mainWindow->show();
    11.  
    12. app.connect( & app, SIGNAL( lastWindowClosed() ),
    13. & app, SLOT( quit() ) );
    14. exitCode = app.exec();
    15.  
    16. return exitCode;
    17. }
    18.  
    19.  
    20. class Secretary : public QMainWindow
    21. {
    22. Q_OBJECT
    23.  
    24. public:
    25. Secretary( QWidget * parent = 0,
    26. Qt::WFlags flags = 0 );
    27. ~Secretary();
    28.  
    29. private:
    30. Ui::SecretaryClass ui;
    31. Secretary_tabMain * tabMain;
    32.  
    33. private slots:
    34. void on_tableThings_clicked( const QModelIndex & item );
    35. void on_tableThings_sectionClicked( int logicalIndex );
    36. };
    37.  
    38.  
    39. Secretary::Secretary( QWidget * parent,
    40. Qt::WFlags flags )
    41. : QMainWindow( parent, flags )
    42. {
    43. ui.setupUi( this );
    44.  
    45. tabMain = new Secretary_tabMain( & ui, this );
    46.  
    47. #if 0 //at run time
    48. QObject::connect( ui.tableThings, SIGNAL( sectionClicked( int ) ),
    49. this, SLOT( on_tableThings_sectionClicked( int ) ) );
    50. #endif //at run time
    51. }
    52.  
    53.  
    54. Secretary::~Secretary()
    55. {
    56. ;
    57. }
    58.  
    59.  
    60. void Secretary::on_tableThings_clicked( const QModelIndex & item )
    61. {
    62. QMessageBox::information( 0, "tableThings", "clicked()" );
    63. }
    64.  
    65.  
    66. void Secretary::on_tableThings_sectionClicked( int logicalIndex )
    67. {
    68. QMessageBox::information( 0, "tableThings", "sectionClicked()" );
    69. }
    To copy to clipboard, switch view to plain text mode 

    On the UI (in Secretary.ui), one of the widgets on the central widget is a QTabWidget, object name tabMain.

    Qt Code:
    1. class Secretary_tabMain : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Secretary_tabMain( Ui::SecretaryClass * UI,
    7. QObject * parent = 0 );
    8. ~Secretary_tabMain();
    9.  
    10. private:
    11. Ui::SecretaryClass * pUI;
    12. Secretary_tabMainThings * tabMainThings;
    13. };
    14.  
    15.  
    16. Secretary_tabMain::Secretary_tabMain( Ui::SecretaryClass * UI,
    17. QObject * parent )
    18. : QObject( parent ),
    19. pUI( UI )
    20. {
    21. tabMainThings = new Secretary_tabMainThings( pUI, this );
    22. }
    23.  
    24.  
    25. Secretary_tabMain::~Secretary_tabMain()
    26. {
    27. ;
    28. }
    To copy to clipboard, switch view to plain text mode 

    The tab widget has several tabs, the tab of interest is a QWidget, object name tabMainThings.

    Qt Code:
    1. class Secretary_tabMainThings : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Secretary_tabMainThings( Ui::SecretaryClass * UI,
    7. QObject * parent = 0 );
    8. ~Secretary_tabMainThings();
    9.  
    10. private:
    11. Ui::SecretaryClass * pUI;
    12. TableViewModel0 * thingsModel;
    13. };
    14.  
    15.  
    16. Secretary_tabMainThings::Secretary_tabMainThings( Ui::SecretaryClass * UI,
    17. QObject * parent )
    18. : QObject( parent ),
    19. pUI( UI )
    20. {
    21. thingsModel = new TableViewModel0;
    22. thingsModel->initView( pUI->tableThings, thingsModel );
    23. }
    24.  
    25.  
    26. Secretary_tabMainThings::~Secretary_tabMainThings()
    27. {
    28. ;
    29. }
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. class TableViewModel0 : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4.  
    5. public :
    6. TableViewModel0( QObject * parent = 0 );
    7. ~TableViewModel0();
    8. void initView( QTableView * view,
    9. TableViewModel0 * model );
    10. };
    11.  
    12.  
    13. TableViewModel0::TableViewModel0( QObject * parent )
    14. : QAbstractTableModel( parent )
    15. {
    16. ;
    17. }
    18.  
    19.  
    20. TableViewModel0::~TableViewModel0()
    21. {
    22. ;
    23. }
    24.  
    25.  
    26. void TableViewModel0::initView( QTableView * view,
    27. TableViewModel0 * model )
    28. {
    29. view->setModel( model );
    30.  
    31. view->setTabKeyNavigation( false );
    32. view->setSortingEnabled( false );
    33. view->setSelectionMode( QAbstractItemView::SingleSelection );
    34. view->setSelectionBehavior( QAbstractItemView::SelectRows );
    35.  
    36. view->horizontalHeader()->setClickable( true );
    37. view->horizontalHeader()->setResizeMode( QHeaderView::ResizeToContents );
    38. view->horizontalHeader()->setHighlightSections( false );
    39. view->horizontalHeader()->setSortIndicatorShown( true );
    40.  
    41. view->verticalHeader()->hide();
    42. view->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
    43. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Unable to catch QHeaderView signals

    sectionClicked() is a signal from QHeaderView
    That's correct.

    You should be getting a warning on the command line: "Object::connect: No such signal tableThings::sectionClicked(int)". Try using QTableView::horizontalHeader() in your connect() statement.

  3. The following user says thank you to norobro for this useful post:

    mule (1st February 2012)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to catch QHeaderView signals

    Quote Originally Posted by mule View Post
    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.
    Automatic connections are made at run time.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #4
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Unable to catch QHeaderView signals

    (Sorry for the silence ... forced off the grid all weekend.)

    norobro, - - -

    QObject::connect( ui.tableThings, SIGNAL( sectionClicked( int ) ), this, SLOT( on_tableThings_sectionClicked( int ) ) );
    does compile, because tableThings is defined at compile time because it is an actual QTableView object. It just doesn't catch the signal.

    Where did you think the reference to horizontalHeader() should be? This:
    QObject::connect( ui.tableThings->horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( on_tableThings_sectionClicked( int ) ) );
    also compiles, but still does not catch the signal.


    wysota, - - -

    I understand that automatic connections are actually made at run time. My statement intended to convey my (possibly incorrect) understanding that the connection table is created at compile time, and therefore the automatic connection would not even be attempted to be made at run time.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Unable to catch QHeaderView signals

    Quote Originally Posted by mule View Post
    I understand that automatic connections are actually made at run time. My statement intended to convey my (possibly incorrect) understanding that the connection table is created at compile time, and therefore the automatic connection would not even be attempted to be made at run time.
    You are contradicting yourself.

    Where did you think the reference to horizontalHeader() should be? This:
    QObject::connect( ui.tableThings->horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( on_tableThings_sectionClicked( int ) ) );
    also compiles, but still does not catch the signal.
    It should have worked (provided that on_tableThings_sectionClicked is declared as a slot). Do you get any warnings about signals on the console at run time?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Unable to catch QHeaderView signals

    norobro,

    You were correct! Adding horizontalHeader() ...
    Qt Code:
    1. QObject::connect( ui.tableThings->horizontalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( on_tableThings_sectionClicked( int ) ) );
    To copy to clipboard, switch view to plain text mode 
    ... DOES work.

    When I initially tried this, I still had one of my "many minor variations" edits of the slot function name, so it couldn't connect.

    Looked at it too many times - Wasn't seeing what I thought I was seeing.
    Too many projects, not enough sleep.

    Thank you.

Similar Threads

  1. Replies: 2
    Last Post: 16th November 2012, 20:41
  2. Replies: 2
    Last Post: 21st August 2011, 07:57
  3. Replies: 6
    Last Post: 29th April 2011, 15:22
  4. Replies: 1
    Last Post: 24th October 2010, 11:09
  5. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 22:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.