Share your knowledge at the LQ Wiki.
Go Back > Blogs > rainbowsally
User Name


Rate this Entry

A stand-alone TextDisplay widget in QT

Posted 12-10-2012 at 03:47 PM by rainbowsally

A stand-alone TextDisplay widget in QT

Skip this part if you know about mc2 (makefile creator, the second generation)

Before we start, if you haven't tried mc2 yet, don't be put off by the installation location. The Computer Mad Science Team uses 4 levels of installation for various kinds of apps.

The most basic type is the HOME/bin sandbox. This allows instantly running applications because that folder is usually in the user's path by default.

The HOME/bin installations have their sources in ~/bin/src where they are easily seen and easily deleted. No hidden folders.

The second level of sandbox installation is the HOME/usr32 type. This is the step before moving stuff into the main system. This installation location allows testing with ZERO CHANCE of creating a bum application by accidentally linking to the wrong libs or includes. If it works here, it will almost certainly work in the "big pond" out there in the linux file system. Next stop is /usr/local once the app proves worthy of becoming globally accessible.

The HOME/usr32 type sand box DOES require ~/usr32/bin in the PATH and ~/usr32/lib in the LD_LIBRARY_PATH.

These can usually be set in ~/.bashrc or whatever your shell uses to pick up its environment modifications.

This kind of installation also usually comes with a import utility in a hidden .update folder just under the top dir of the installer that can be used to import changes made in the installed sources back into the installer.

Remember to change the version numbers.

The third and fourth types are make-based and don't apply to our own utilities and libs.

Continue? :-)

Let's experiment a bit.

The latest LQ-qt-mc2 download (version >= 3.0.12) is recommended for this because it has a few bug fixes and tweeks for messing with QT4 that you're likely to want if you try this experiment.

1. Create a new Widget from a template in qt designer.

2. Change the widget's name to "TextEdit" at the top in designer's property editor.

3. Save it as textdisplay.ui

4. Now, with a TEXT editor like kwrite open textdisplay.ui and change

the widget class from QWidget to QTextEdit
<widget class="QWidget" name="TextDisplay">
to this
<widget class="QTextEdit" name="TextDisplay">

You can also change the window title while you're at it, or just proceed.

5. Reopen the textdisplay.ui file in Designer and verify that it still opens and that you *now have a TextEdit* where there was once nothing a blank widget.

Change the in the ui file using designer (much better than 'creator' in my opinion).

QTextEdit readonly property to [\,] (checked)

because it's a TextDisplay only, and save it again.

This adds the following to the ui file.
<property name="readOnly">

Now let's play with it.

[If you don't have mc2 you can try to figure out what this does, but...]

Run mc2 in the directory with your ui file
mc2 -fetch qt4
to generate the first generation of the makefile.

Then run
mc2 -create qt4-files ./textdisplay.ui
and copy the ui and clean-ui targets into mc2.def and change SRCDIR to '.'

This creates the class, the header the ui_class and moc_class c++ and headers as well as app-main.cpp which we'll look at first.

You can also remove the src subdirectory now because we aren't using it.

Now for the code changes:

1. In app-main.cpp

#include "ui_textdisplay.h"
So we can access the inner fields of the class.

Add below show() in the user definitions:
Here's my app-main.cpp

file: app-main.cpp
purpose: source file
#include <QtGui/QApplication>
#include "textdisplay.h"
#include "ui_textdisplay.h"

void dbg(){}

int main(int argc, char *argv[])
  // this sets the global qApp instance
  new QApplication (argc, argv);
  TextDisplay w;;
  // Reposition, resize etc. here after shown()

  return qApp->exec();
Notice that 'self' is used as a pointer rather than the usual 'ui' pointer that QT thinks we should use.

Now 'self' *must be* a QTextEdit pointer, and as we know from the old C days that if it talks like a duck... Cast it to duck and you're on your way.

So now we need to make 'self' a pointer to our widget as a TextEdit widget, and here's how we can do that.

2. In the non-volatile header textdisplay.h

In the public section near the top add
   QTextEdit* self;
And now we have a pointer to the TextEdit functions for our TextDisplay.

3. In the non-volatile source file textdisplay.cpp

In the constructor below ththe setup:
add the waddle back into our duck.
    self = (QTextEdit*)this;
4. Test..
  make clean; make update; make
And if the name is still 'main' in the mc2.def file, type 'main' and it shouldrun.

You should see a stand-alone text display showing the text you put into itand you can copy, but you cannot write anything into it.

If you want buttons to close the app with, you need to go the 'normal' route and add them to a container class of some sort.

  • If the linker can't find '__start' make sure SRCDIR = '.'. in the mc2.def file. mc2 isn't finding app-main.cpp.
  • If the textdisplay widget pointer can't be used for 'self', make sure you added the
  • #include "ui_textdisplay.h" in order to get at all the classes fields.
  • If something else is wrong, try removing one file at a time and re-running
  • mc2 -create qt4-files
  • adding the modifications as per above, as needed... until it works. (This process is infinitely less annoying with mc2 version >= 3.1.12.)
  • Or restart in a new directory, making sure you got the 'mc2 -fetch qt4' definitions. It the app does work.

Now "What in the world is something like this good for?", you might ask.

"We haven't got a clue."
-- The Computer Mad Science Team

[Actually there is a possible use for it, but we can look into that later... -rs]
Posted in Uncategorized
Views 995 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 05:56 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration