PDA

View Full Version : How QMessageBox can be inherited.



rajveer
4th September 2008, 12:51
i have displayed a message by using QMessageBox and i have done all the translation related to my message but the Yes / No option cannot be translated. So how can it be done.

spirit
4th September 2008, 13:14
you can get access to dialog buttons using this method
http://doc.trolltech.com/4.4/qmessagebox.html#button

so, in you case you need to do


...
msgBox.button(QMessageBox::Yes)->setText(tr(....));
...

rajveer
5th September 2008, 06:46
i have added a message in message box box , but how the Ok can be changed

spirit
5th September 2008, 06:47
this code didn't help?


...
msgBox.button(QMessageBox::Ok)->setText(tr(....));
...

rajveer
5th September 2008, 07:03
yeah it wont work.

spirit
5th September 2008, 07:09
this code works fine


QMessageBox msg(QMessageBox::Information, tr("information"), tr("Some inportant information"),
QMessageBox::Ok | QMessageBox::Yes | QMessageBox::Save | QMessageBox::Discard);
msg.button(QMessageBox::Ok)->setText(tr("My Ok label"));
msg.button(QMessageBox::Yes)->setText(tr("My Yes label"));
msg.button(QMessageBox::Save)->setText(tr("My Save label"));
msg.button(QMessageBox::Discard)->setText(tr("My Discard label"));
msg.exec();

rajveer
5th September 2008, 07:43
i did it but it said tr identifier not found.

spirit
5th September 2008, 07:44
use QObject::tr(...)

rajveer
5th September 2008, 08:05
how i can use it with set text.

and also it shows "use of undefined type Qabstract button" with button means msgBox.button

spirit
5th September 2008, 08:09
how i can use it with set text.
see http://doc.trolltech.com/4.4/qobject.html#tr



and also it shows "use of undefined type Qabstract button" with button means msgBox.button

include

#include <QAbstractButton>

rajveer
5th September 2008, 08:19
msg.button(QMessageBox::Ok)->setText(tr("My Ok label"));

here it shows tr identifier not found.
so here how can i use QObject.

spirit
5th September 2008, 08:23
:eek:


...
msg.button(QMessageBox::Ok)->setText(QObject::tr("My Ok label"));
...

rajveer
8th September 2008, 07:10
I m doing like this:


void MyMessageBox: QMessageBox(QWidget* parent, const QString& title, const QString& text, const QMessageBox&)
{
QMessageBox box;
box.removeButton("OK");
box.addButton("OK");
box.exec();
return;

it is not working.

please help me out.

aamer4yu
8th September 2008, 08:55
MyMessageBox: QMessageBox

I guess u are inheriting from QMessageBox,,,
then why are u making another instance of QMessageBox inside the ctor ?? :confused:

spirit
8th September 2008, 10:04
I m doing like this:

void MyMessageBox: QMessageBox(QWidget* parent, const QString& title, const QString& text, const QMessageBox&)
{
QMessageBox box;
box.removeButton("OK");
box.addButton("OK");
box.exec();
return;

it is not working.

please help me out.
I don't understand what do you want to do. :eek:

rajveer
9th September 2008, 08:33
my mentor said u have to create ur own class and derive it from QMessageBox. After dat then u have to remove the already added button and add ur own buttons.

aamer4yu
9th September 2008, 09:18
What do you want to do anyway...
in the first post ur prob was regaarding translation. Now its something else.
If you can tell what u want to achieve, may be we can tell a way..

spirit
9th September 2008, 09:34
I asked about your code. nevermind.
example


----h----
#ifndef TEST_H
#define TEST_H

#include <QMessageBox>

class Test: public QMessageBox
{
Q_OBJECT

public:
Test(QMessageBox::Icon icon, const QString &title,
const QString &text,
QMessageBox::StandardButtons buttons = NoButton,
QWidget *parent = 0,
Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
~Test();
};

#endif
---cpp---
#include "test.h"

Test::Test(Icon icon, const QString &title,
const QString &text, StandardButtons buttons,
QWidget *parent, Qt::WindowFlags f)
:QMessageBox(icon, title, text, buttons, parent, f)
{
QAbstractButton *ok = button(Ok);
removeButton(ok);
addButton(tr("MyButton"), AcceptRole);
}

Test::~Test()
{
}

---another_cpp_code---
....
Test test(QMessageBox::Information, QObject::tr("information"),
QObject::tr("Some important information"), QMessageBox::Ok | QMessageBox::No);
test.exec();
....

I don't see any trouble. is it hard to you?

rajveer
9th September 2008, 10:31
an error is coming:

'void QMessageBox::addButton(QAbstractButton *,QMessageBox::ButtonRole)' : cannot convert parameter 1 from 'QString' to 'QAbstractButton *'

spirit
9th September 2008, 10:37
http://doc.trolltech.com/4.4/qmessagebox.html#addButton-2

rajveer
9th September 2008, 10:46
but that was used with QPushButton, but in ur code u are using QAbstractButton. So the problem is coming when we use tr.
that is:

addButton(tr("OK"), QMessageBox::Ok);

error is:

'void QMessageBox::addButton(QAbstractButton *,QMessageBox::ButtonRole)' : cannot convert parameter 1 from 'QString' to 'QAbstractButton *'

spirit
9th September 2008, 10:47
I built this code before post it.

rajveer
9th September 2008, 10:50
so now what should i do. pls help me.

spirit
9th September 2008, 10:53
try this project

rajveer
9th September 2008, 11:02
hello sir, i m doing just like this, but still it is giving the same error.

spirit
9th September 2008, 11:08
ok, I don't understand why this happens.:eek: anyway, try this insted


...
QPushButton *myOk = new QPushButton(tr("Ok"));
addButton(myOk, AcceptRole);
...

rajveer
9th September 2008, 12:17
now two errors r cuming.

1) 'QPushButton' : class has no constructors
2)'void QMessageBox::addButton(QAbstractButton *,QMessageBox::ButtonRole)' : cannot convert parameter 1 from 'QPushButton *' to 'QAbstractButton *'

spirit
9th September 2008, 12:23
include QPushButton or just QtGui.

aamer4yu
9th September 2008, 12:27
What Qt version are u using ? Are ur paths properly included ?

I tried the test.zip, and it works fine without any error. I have Qt 4.3.4.

rajveer
9th September 2008, 12:29
what abt this error:

'void QMessageBox::addButton(QAbstractButton *,QMessageBox::ButtonRole)' : cannot convert parameter 2 from 'QMessageBox::StandardButton' to 'QMessageBox::ButtonRole'

rajveer
9th September 2008, 12:30
i have used 4.3.2

aamer4yu
9th September 2008, 12:40
Where are u getting that error ?
What i did was extracted the test.zip, ran qmake -tp vc , and built the project from visual studio. No errors.

I dont know what u are doing... u must be missing something

rajveer
9th September 2008, 12:55
i m just doing the same , but it is giving an error which i have already told.

wysota
9th September 2008, 20:56
i have displayed a message by using QMessageBox and i have done all the translation related to my message but the Yes / No option cannot be translated. So how can it be done.

It's enough to provide translations for the proper context and message. Look into the source code of QMessageBox to see what it expects. There is really no need to making things more complex than they really are.

rajveer
10th September 2008, 08:00
Its not working , how the QMessageBOx button can be translated.

wysota
10th September 2008, 08:05
What is not working? What exactly did you do?

spirit
10th September 2008, 08:07
use QObejct::tr and Qt Linguist (http://doc.trolltech.com/4.4/linguist-manual.html) for translation.

rajveer
10th September 2008, 08:24
i have to change all the buttons which are coming when we are using QMessageBox in the application . the text message is changed by using tr but the OK , yes, No buttons can not be changed. so how can i do this?

my mentor said u hav to create a class and derive it from QMessageBox and then remove that buttons and add ur own buttons. but how can i do that. means i have using it in one way which the other sir told me in the forum.

wysota
10th September 2008, 09:10
You don't have to do anything like that. Read my post again, it says exactly what you need to do and believe me it will work as Qt does it exactly the same when it translates the message box with a proper translator installed.

tone
10th September 2008, 10:54
My distribution of Qt has translation files for a bunch of languages, which translates the texts and labels of the standard Qt buttons (and possibly other standard stuff as well).
If you install a translator loaded with the correct file your standard buttons will have their text translated for you.
Would that answer your needs?
in my PC the translations are located in C:\Qt\4.4.0\translations\*
Pick the one that suits you and then install it using this code:

QString trans_file("<translation_file_path>");
trans_file = trans_file.toLower();
QTranslator qtTranslator;
qtTranslator.load(trans_file);
qApp.installTranslator(&qtTranslator);
cheers,
--to

tone
10th September 2008, 14:27
rajveer,
Please keep the thread converstion here in the thread and not through PMs so that everyone can benefit it, in case we manage to help you solve your problem...

Moving on....
The code that I posted could (or should?) be inserted in your main(), after you create your own instance of QApplication (or some subclass that inherits it).
qApp is a Qt "builtin" that points to that instance.
The file I mentioned has a .qm extension. For instance, if the file name is qt_martian.qm, you can pass only qt_martian without the .qm and the correct file name will be inferred when you try to load it.

What it does is install the translator that translates the strings of the standard buttons and other Qt stuff.

I'm not very knowledgeable in Qt but this is basically what the thing does. Someone with more insight can stop by and correct any mistake I said.

Have a look at http://doc.trolltech.com/4.4/i18n.html there's a lot to learn from there.

cheers,
--to