PDA

View Full Version : How to show QLabels



Pavel Abrosimov
2nd July 2009, 18:23
Hello everybody.

There is a problem with showing QLabels.

There is a QPlugin made as dll. It has an array:


typedef boost::shared_ptr< QLabel > TLabel;
typedef std::vector< TLabel > TLabels;
typedef TLabels::iterator TLabelsIter;
typedef TLabels::const_iterator TLabelsIterConst;


and so in the plugin's class:


TLabels labels;


During the work of the plugin, next initialization present:


TLabel newLabel;
newLabel.reset(new QLabel);
QFont newFont("Serif", 12);
newLabel->setFont(newFont);
QPalette pal( "#FFFF00");
newLabel->setPalette(pal);
newLabel->move(10, 10);

labels.push_back(pLabel);


The there is a code which simply changes text in the label:


labels[j]->setText(dataQStr);


The plugin's class has QWidget inheritance.

When the program starts, no labels are seen.

If I add to the plugin's class an element:


private:
QMainWindow mainWindow;


And initialize it after with:



mainWindow.show();


When the program starts, there is a window with NULL information in it.

What do I need to do, in order to show the labels I have in the mainWindow?
or is there any ways to do it without the mainWindow element?

Thanks a lot in advance.

wysota
2nd July 2009, 18:37
How do you add the labels to the window?

Pavel Abrosimov
2nd July 2009, 18:42
I don't really.

faldzip
2nd July 2009, 19:26
so you have to show your labels. You can do it by calling show() on them, but it is not a proper way. First of all adding the window as parent for label should show them within the window. But it is prefered to use a layout to add and position the labels within the window.

P.S. And you can use Qt containers such a QList and there is also very nice Qt keyword such as foreach.

wysota
2nd July 2009, 21:10
I don't really.

You have to add them to the layout of the central widget of the main window.

Pavel Abrosimov
2nd July 2009, 22:32
It is really needed to use the system of coordinates for the labels (x and y from the top left corner). There is such a function in QLabels (move(int x, int y)). How would it be possible to use with the layout (I know about QHBoxLayout, but I am not sure about movement inside it)

Thanks a lot for all the answers.

P.S.

About QList.
Isn't it just an another kind of arrays? I am asking that, because it seams to be needed to add all the labels to the mainWindow (to make them children of the mainWindow), if I understood it correct. If not, how that would iteract with the mainWindow (how would mainWindow be aware about created qlist)?

wysota
2nd July 2009, 22:49
Hmm... all what you write above makes me think you are trying to use the labels in a wrong way. Could you explain what is that you are trying to achieve?

Pavel Abrosimov
2nd July 2009, 23:07
I need to show some information above the video. The window needs to be full screen and transparent. The last two options are seems to be achievable.

wysota
2nd July 2009, 23:56
Maybe you should use QGraphicsView instead of going through hell with labels? An alternative is to provide an overlay over the video (although I'm not sure this is possible if you're planning to use Phonon) and use QPainter to do the drawing.

Pavel Abrosimov
3rd July 2009, 00:18
To be honest, work needs to be done with QGraphicsView is unknown. We are not going to use phonon. Is there easy way to attach those labels to that mainWindow and have an ability to call move(int x, int y) on them?

wysota
3rd July 2009, 00:40
Just don't put them into layouts although it'd really be simpler with the graphics view. And if you're going to show the video full screen, you'll probably want to use QWidget instead of QMainWindow.

Pavel Abrosimov
3rd July 2009, 00:43
The class, from which qtplugin is made of is already a QWidget. I need some window. I just didn't know how else can I do it, apart from calling QMainWindow.

aamer4yu
3rd July 2009, 09:14
Even QWidget can be used to hold labels and be shown as a window.
Something like -

main()
{
....
// get labels from your plugin.
QWidget window;
// add ur labels to window. The position u set is relative to window.
window.show();
....
}

Pavel Abrosimov
3rd July 2009, 10:10
aamer4yu Thanks for information.

So if I would call on them during initialization:


TLabel label(new QLabel(this));
label->move(x, y);
labels.bush_back(label);
...
show();



and all the labels would be pointers:


typedef QLabel* TLabel;

There is a white window with nothing in it. Am I missing something?
A call label->show(); doesn't change anything.

wysota
3rd July 2009, 12:12
Can you prepare a minimal compilable example that does what your code does so that we can work on it?

Pavel Abrosimov
3rd July 2009, 12:31
header:


typedef QLabel* TLabel;
typedef std::vector< TLabel > TLabels;
typedef TLabels::iterator TLabelsIter;
typedef TLabels::const_iterator TLabelsIterConst;

class MYclass:
public QWidget,
{
Q_OBJECT
Q_INTERFACES(NTlParser::IBase NTlParser::ICfg NTlParser::IDataConsumer)

public:
MYclass(QWidget *parent = 0);
~MYclass();
void doSmth();
private:
void AssertLabels();
TLabels labels;

};


in source:


MYclass::MYclass(QWidget *parent)
: QWidget(parent)
{
}
MYclass::~MYclass()
{
}

void MYclass::AssertLabels()
{
for(uint i = 0; i < 4; ++i){
TLabel pLabel(new QLabel(this));
QFont newFont("Serif", 12);
pLabel->setFont(newFont);
QPalette pal("#FFFF00");
pLabel->setPalette(pal);
pLabel->move(10, 10 + i * 20);
pLabel->setText("Fake text");

labels.push_back(pLabel);
}
show();//need to show them, right?
}

void MYclass::doSmth()
{
for(uint i = 0; i < labels.size(); ++i){
labels[i]->setText("New Fake text");
}
}



Something like that

wysota
3rd July 2009, 12:38
I meant something I could compile and run, not "something like that" :)

Anyway, do you call AssertLabels() anywhere? And what's the purpose of using that TLabel?

Pavel Abrosimov
3rd July 2009, 12:43
sorry, forgot to add:



MYclass::MYclass(QWidget *parent)
: QWidget(parent)
{
AssertLabels();
}


Unfortunately, there is nothing I could give and it would compile straight away :(

But the code I posted is very close to what I need to achieve (some methods of parameters and quantities are different)

Thanks a lot for your help

Pavel Abrosimov
4th July 2009, 22:45
Ok. The problem is solved by doing next:
Add the paintEvent(QPaintEvent *) and its description in source file:

void MYclass::paintEvent(QPaintEvent *)
{
QPainter painter(this);
}

Thanks everyone for help.

wysota
4th July 2009, 23:02
Ok. The problem is solved by doing next:
Add the paintEvent(QPaintEvent *) and its description in source file:

void MYclass::paintEvent(QPaintEvent *)
{
QPainter painter(this);
}



Hmm.... you really shouldn't do that... If you need something like this then it means you were not deriving from QWidget.

Pavel Abrosimov
8th July 2009, 12:42
You could be right. But the class in the source code has inheritance from QWidget.
The QLabels didn't show with that properly as it was only first letter was seen from any label.

The trick with QPainter did it's job - there is a loop for all required text and painter draws it:

painter.drawText(x, y, text);

Draw text with different color and different size is not a big deal. With color seems to be fine - just call painter.setPen(color). And after - painter.setFont(font)

But now I am facing new problems - how can I make the window with all that information to be transparent?

Best regards

Pavel

wysota
8th July 2009, 13:29
The QLabels didn't show with that properly as it was only first letter was seen from any label.

The trick with QPainter did it's job - there is a loop for all required text and painter draws it:

painter.drawText(x, y, text);
So what are the labels for if you're drawing the text manually?

To me the problem seems to be caused by the fact that you are not using layouts and that you completely ignore setting geometry of the labels, so it's quite obvious you see nonsense.


#include <QLabel>
#include <QApplication>


int main(int argc, char **argv){
QApplication app(argc, argv);
QWidget w;
for(int i=0;i<10;i++){
QLabel *l = new QLabel(&w);
l->setText(QString("Label %1").arg(i+1));
l->setGeometry(i*20, i*20, l->sizeHint().width(), l->sizeHint().height());
}
w.show();
return app.exec();
};

Pavel Abrosimov
8th July 2009, 14:35
As you said, there is no need in QLabels. And they are all taken away. There is just a sort of vector with all QStrings needed.

How it is possible to make the background of the window transparent, but the text must be visible on it?

wysota
8th July 2009, 18:52
Please search the forum. The issue has been brought up multiple times recently.

Pavel Abrosimov
9th July 2009, 10:39
wysota thank you very much for your help.

Best regards

Pavel