Results 1 to 8 of 8

Thread: QLabel in QStatusbar

  1. #1
    Join Date
    Apr 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QLabel in QStatusbar

    I have a statusbar and I have added two QLabels in the statusbar. I have a callback function and the text I am setting is not showing up. I know my callback functions ok, because if I change the parameter from a QLabel to a QStatusbar the text displays. I am displaying the lat/long in the status bar, but would like to get it to display in my QLabel that is added to the statusbar.

    bool
    MouseCoordsUtility::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
    {
    if (ea.getEventType() == ea.MOVE || ea.getEventType() == ea.DRAG)
    {
    osg::Vec3d world;
    if ( m_mapNode->getTerrain()->getWorldCoordsUnderMouse(aa.asView(), ea.getX(), ea.getY(), world) )
    {
    GeoPoint map;
    map.fromWorld( m_mapNode->getMapSRS(), world );

    for( Callbacks::iterator i = m_callbacks.begin(); i != m_callbacks.end(); ++i )
    i->get()->set( map, aa.asView(), m_mapNode );
    }
    else
    {
    for( Callbacks::iterator i = m_callbacks.begin(); i != m_callbacks.end(); ++i )
    i->get()->reset( aa.asView(), m_mapNode );
    }
    }

    return false;
    }

    void MouseCoordsUtilLabelCallback::set(const GeoPoint& mapCoords,osg::View* view, MapNode* mapNode)
    {
    if(m_label)
    {
    std::string result;
    result = osgEarth::Stringify() << m_formatter->format(mapCoords) << ", " << mapCoords.z();
    //result = "This is a test.";
    //m_label->showMessage(QString(result.c_str()));
    m_label->setText(QString(result.c_str()));
    }
    }

    This is my set function and the function that calls it. Hopefully that will give you some idea.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QLabel in QStatusbar

    How have you put m_label on the QStatusBar?

  3. #3
    Join Date
    Apr 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel in QStatusbar

    Yes. I add the QLabel to my statusbar. Then I pass that QLabel that I create to the function that sets the text.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QLabel in QStatusbar

    I asked how you put it in the status bar, not whether you did or think you did. Clearly one of these applies:
    • You didn't add the label to the status bar.
    • You add a label other than the one you think you have.
    • You delete the label object after adding it.
    • You are setting text on a label other than the one you added to the status bar

    Otherwise the text would be visible. Your code does not show where you initialise m_label, add it to the status bar, manipulate afterward etc... which is why I asked.

    Here is an example to convince you it does work
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class MainWindow: public QMainWindow {
    5. Q_OBJECT
    6. public:
    7. MainWindow(QWidget *p = 0): QMainWindow(p), n(0) {
    8. label = new QLabel(this);
    9. statusBar()->addWidget(label);
    10.  
    11. QTimer *timer = new QTimer(this);
    12. connect(timer, SIGNAL(timeout()), SLOT(tick()));
    13. timer->start(500);
    14. }
    15. public slots:
    16. void tick() {
    17. label->setText(QString::number(++n));
    18. }
    19. private:
    20. int n;
    21. QLabel *label;
    22. };
    23.  
    24. int main(int argc, char **argv) {
    25. QApplication app(argc, argv);
    26. MainWindow m;
    27. m.show();
    28. return app.exec();
    29. }
    30. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel in QStatusbar

    Ok. Posted more of the code. Constructor create

    Qt Code:
    1. MainWindow::MainWindow( osgViewer::ViewerBase::ThreadingModel threadingModel ) : QMainWindow()
    2. {
    3. setThreadingModel(threadingModel);
    4.  
    5. //QWidget* widget1 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("/home/hallmw/Downloads/gwaldron-osgearth-6bc5b12/tests/boston.earth") );
    6.  
    7. // Read in the file and create our m_mapNode
    8. osg::Node* node = osgDB::readNodeFile("Data/bmng.earth");
    9.  
    10. m_mapNode = MapNode::findMapNode(node);
    11.  
    12. // Create our widget to view our scene.
    13. QWidget* widget1 = addViewWidget( createCamera(0,0,100,100), node);
    14.  
    15. infoLabel = new QLabel("Right", this);
    16. infoLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    17.  
    18. m_latlongStatus = new QLabel("Middle", this);
    19. m_latlongStatus->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    20.  
    21. // Sets the central widget to the widget we created
    22. setCentralWidget(widget1);
    23.  
    24. // Create our actions
    25. createActions();
    26.  
    27. // Create our Menus
    28. createMenus();
    29.  
    30. // Create the status bar
    31. createStatusBar();
    32.  
    33. //updateStatusBar();
    34.  
    35. connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
    36. _timer.start( 10 );
    37. }
    38.  
    39. QWidget* MainWindow::addViewWidget( osg::Camera* camera, osg::Node* scene )
    40. {
    41. mouseUtil = new MouseCoordsUtility(m_mapNode, m_latlongStatus);
    42. osgViewer::View* view = new osgViewer::View;
    43. view->setCamera( camera );
    44. addView(view);
    45.  
    46. view->setSceneData( scene );
    47. view->addEventHandler( new osgViewer::StatsHandler );
    48. view->addEventHandler(mouseUtil);
    49. view->setCameraManipulator( new osgGA::TrackballManipulator );
    50.  
    51. osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>( camera->getGraphicsContext() );
    52. return gw ? gw->getGLWidget() : NULL;
    53. }
    54.  
    55. // Creates the status bar for the application.
    56. void MainWindow::createStatusBar()
    57. {
    58. statusBar()->addWidget(infoLabel,1);
    59. statusBar()->addWidget(m_latlongStatus,2);
    60. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MouseCoordsUtilLabelCallback::MouseCoordsUtilLabelCallback(QLabel* statusbar, Formatter* formatter)
    2. {
    3. m_label = statusbar;
    4. m_formatter = formatter;
    5.  
    6. if(!formatter)
    7. {
    8. m_formatter = new LatLongFormatter(LatLongFormatter::FORMAT_DEGREES_MINUTES_SECONDS);
    9. }
    10. }
    11.  
    12. bool
    13. MouseCoordsUtility::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
    14. {
    15. if (ea.getEventType() == ea.MOVE || ea.getEventType() == ea.DRAG)
    16. {
    17. osg::Vec3d world;
    18. if ( m_mapNode->getTerrain()->getWorldCoordsUnderMouse(aa.asView(), ea.getX(), ea.getY(), world) )
    19. {
    20. GeoPoint map;
    21. map.fromWorld( m_mapNode->getMapSRS(), world );
    22.  
    23. for( Callbacks::iterator i = m_callbacks.begin(); i != m_callbacks.end(); ++i )
    24. i->get()->set( map, aa.asView(), m_mapNode );
    25. }
    26. else
    27. {
    28. for( Callbacks::iterator i = m_callbacks.begin(); i != m_callbacks.end(); ++i )
    29. i->get()->reset( aa.asView(), m_mapNode );
    30. }
    31. }
    32.  
    33. return false;
    34. }
    35.  
    36. void MouseCoordsUtilLabelCallback::set(const GeoPoint& mapCoords,osg::View* view, MapNode* mapNode)
    37. {
    38. if(m_label)
    39. {
    40. std::string result;
    41. result = osgEarth::Stringify() << m_formatter->format(mapCoords) << ", " << mapCoords.z();
    42. //result = "This is a test.";
    43. //m_label->showMessage(QString(result.c_str()));
    44. m_label->setText(QString(result.c_str()));
    45. }
    46. }
    To copy to clipboard, switch view to plain text mode 

    The two labels are created in the constructor. They are added to the statusbar in createStatusBar.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QLabel in QStatusbar

    Do you pass a valid pointer into your MouseCoordsUtilLabelCallback constructor?
    When you single step this in your debugger is m_label zero in MouseCoordsUtilLabelCallback::set() ?
    Do you have threads involved at all?

  7. #7
    Join Date
    Apr 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel in QStatusbar

    Well I will have to learn to use the debugger. I am just using a text editor and have not really ever used the linux debugger. Good time to learn.

  8. #8
    Join Date
    Apr 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QLabel in QStatusbar

    No I am not using any threading. I can single step in the debugger and it executes the code inside the if statement. That leads me to believe that the m_label variable is not 0.

Similar Threads

  1. QLabel in QStatusBar does freaky stuff
    By dsab123 in forum Newbie
    Replies: 2
    Last Post: 21st June 2012, 07:26
  2. Replies: 1
    Last Post: 29th September 2009, 19:44
  3. Why QStatusBar do not at the bottom?
    By FinderCheng in forum Qt Programming
    Replies: 5
    Last Post: 29th September 2009, 08:26
  4. QStatusBar
    By newermind in forum Qt Programming
    Replies: 3
    Last Post: 1st June 2009, 21:48
  5. about QStatusBar
    By Pang in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2006, 04:15

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.