×
Namespaces

Variants
Actions

Archived:Using QLabel instead of QTextBrowser

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Qt Quick should be used for all UI development on mobile devices. The approach described in this article (using C++ for the Qt app UI) is deprecated.

Article Metadata
Code ExampleTested with
Devices(s): 5800 XpressMusic
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: QLabel, usage
Created: taaidant (04 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Overview

This code demonstrates how to display more than one line of text in QLabel without having to use QTextBrowser.

Header file

/*
* Copyright (c) 2009 Nokia Corporation
*/

 
#ifndef QLABELSNIPPET_H
#define QLABELSNIPPET_H
 
#include <QtGui/QMainWindow>
#include <QtGui/QFrame>
#include <QtGui/QLabel>
#include <QtGui/QVBoxLayout>
 
#include <QtCore/QPointer>
#include <QtCore/QString>
 
class QLabelSnippet : public QMainWindow
{
Q_OBJECT
 
public:
QLabelSnippet(QWidget *parent = 0);
~QLabelSnippet();
 
private:
QPointer<QLabel> _textContainer;
};
 
#endif // QLABELSNIPPET_H

Source file

/*
* Copyright (c) 2009 Nokia Corporation
*/

 
#include "qlabelsnippet.h"
 
QLabelSnippet::QLabelSnippet(QWidget *parent) : QMainWindow(parent) {
/* Label we will be enabling the copy & paste in. */
_textContainer = new QLabel;
/* We'll make it not use only one line. */
_textContainer->setWordWrap(true);
/* Contents can be scaled. */
_textContainer->setScaledContents(true);
/*
* Adjust to the size of the layout.
* http://doc.trolltech.com/qsizepolicy.html
*/

_textContainer->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
/* We'll give it a border so you can see how it grows.*/
_textContainer->setStyleSheet("border: 1px solid black;");
/*
* Enable text selection and link accessing.
* http://doc.trolltech.com/qt.html#TextInteractionFlag-enum
*/

_textContainer->setTextInteractionFlags(Qt::TextBrowserInteraction);
/* Let's align it to the top. */
_textContainer->setAlignment(Qt::AlignTop);
/* Let's put some lorem ipsum in it. */
QString text = QString("Lorem ipsum dolor sit amet, consectetur");
text.append("adipiscing elit. Ut a felis mauris. Fusce ");
text.append("odio sapien, rutrum eu tincidunt eu, luctus ");
text.append("vel libero. Praesent viverra tincidunt ipsum, ");
text.append("nec dictum augue scelerisque in. Integer quis ");
text.append("consectetur nisi. Phasellus ac bibendum arcu. ");
text.append("Vivamus elementum cursus ligula, quis volutpat ");
text.append("mi tincidunt quis. Fusce nec dui id tellus tristique ");
text.append("luctus in sit amet erat. In volutpat diam nec libero ");
text.append("scelerisque lobortis. Maecenas dolor quam, egestas at ");
text.append("pretium nec, placerat eget leo. Sed ac ultricies enim. ");
text.append("Nulla eget est tellus. In accumsan felis in tortor ");
text.append("adipiscing eget pretium metus elementum. Ut id eros ");
text.append("lorem. Nam interdum ipsum eu neque bibendum pretium. ");
text.append("Etiam vitae congue massa.");
_textContainer->setText(text);
/* Displaying widgets is so much easier if we use a layout... */
QVBoxLayout* layout = new QVBoxLayout;
/* Let's add the widget to the layout */
layout->addWidget(_textContainer);
/* Now we need a central widget to act as our container */
QFrame* frame = new QFrame(this);
/* And set the layout as the container layout to get it displayed. */
frame->setLayout(layout);
/* Now we set the container as our central widget. */
setCentralWidget(frame);
}
 
QLabelSnippet::~QLabelSnippet() {
this->_textContainer->deleteLater();
}

Postconditions

You are able to use QLabel to contain long textual data, make it selectable, and get it to scale.

Supplementary material

  • You can test QLabelSnippet with a test application. The application is available for download at Media:QLabelSnippet.zip.

See also

This page was last modified on 11 October 2012, at 04:15.
64 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×