PDA

View Full Version : QLabel in QStatusbar



hallmw
12th April 2013, 03:10
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.

ChrisW67
12th April 2013, 04:39
How have you put m_label on the QStatusBar?

hallmw
14th April 2013, 00:40
Yes. I add the QLabel to my statusbar. Then I pass that QLabel that I create to the function that sets the text.

ChrisW67
14th April 2013, 01:04
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

#include <QtGui>
#include <QDebug>

class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *p = 0): QMainWindow(p), n(0) {
label = new QLabel(this);
statusBar()->addWidget(label);

QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), SLOT(tick()));
timer->start(500);
}
public slots:
void tick() {
label->setText(QString::number(++n));
}
private:
int n;
QLabel *label;
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
MainWindow m;
m.show();
return app.exec();
}
#include "main.moc"

hallmw
14th April 2013, 03:30
Ok. Posted more of the code. Constructor create


MainWindow::MainWindow( osgViewer::ViewerBase::ThreadingModel threadingModel ) : QMainWindow()
{
setThreadingModel(threadingModel);

//QWidget* widget1 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("/home/hallmw/Downloads/gwaldron-osgearth-6bc5b12/tests/boston.earth") );

// Read in the file and create our m_mapNode
osg::Node* node = osgDB::readNodeFile("Data/bmng.earth");

m_mapNode = MapNode::findMapNode(node);

// Create our widget to view our scene.
QWidget* widget1 = addViewWidget( createCamera(0,0,100,100), node);

infoLabel = new QLabel("Right", this);
infoLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);

m_latlongStatus = new QLabel("Middle", this);
m_latlongStatus->setFrameStyle(QFrame::Panel|QFrame::Sunken);

// Sets the central widget to the widget we created
setCentralWidget(widget1);

// Create our actions
createActions();

// Create our Menus
createMenus();

// Create the status bar
createStatusBar();

//updateStatusBar();

connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
_timer.start( 10 );
}

QWidget* MainWindow::addViewWidget( osg::Camera* camera, osg::Node* scene )
{
mouseUtil = new MouseCoordsUtility(m_mapNode, m_latlongStatus);
osgViewer::View* view = new osgViewer::View;
view->setCamera( camera );
addView(view);

view->setSceneData( scene );
view->addEventHandler( new osgViewer::StatsHandler );
view->addEventHandler(mouseUtil);
view->setCameraManipulator( new osgGA::TrackballManipulator );

osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>( camera->getGraphicsContext() );
return gw ? gw->getGLWidget() : NULL;
}

// Creates the status bar for the application.
void MainWindow::createStatusBar()
{
statusBar()->addWidget(infoLabel,1);
statusBar()->addWidget(m_latlongStatus,2);
}




MouseCoordsUtilLabelCallback::MouseCoordsUtilLabel Callback(QLabel* statusbar, Formatter* formatter)
{
m_label = statusbar;
m_formatter = formatter;

if(!formatter)
{
m_formatter = new LatLongFormatter(LatLongFormatter::FORMAT_DEGREES_ MINUTES_SECONDS);
}
}

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()));
}
}

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

ChrisW67
14th April 2013, 07:27
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?

hallmw
18th April 2013, 00:52
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.

hallmw
18th May 2013, 17:31
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.