Set specific style to a certain QMessageBox - c++

How to set a specific style to a certain QMessageBox in Qt. Particularly, I would like to invoke a message box which is written below by setting a font size and color for the text "Well done!!!".
QMessageBox::information(this, "Done", "Well done!!!");

Using this static method you can not access QMessageBox directly, because its created and destroyed entirely inside function. But you may try to alter stylesheet property of calling object before call to this function. This means calling object should be descedant of QWidget...
something like this:
QString tmpStyleSheet=this->styleSheet();
this->setStyleSheet("style sheet for your QMessageBox");
QMessageBox::information(this, "Done", "Well done!!!");
this->setStyleSheet(tmpStyleSheet);
style of QMessageBox will inherit parent's style...

You will need to create a QMessageBox object for that particular message box and then either setStyle() or setStyleSheet(), in other words you cannot use the static helper method.

I have formated the text of the QMessageBox with HTML code. Sorry for the question. And thanks for answers.

Related

How to get the text value of a control through its parent window

I have the following wxDialog parent window:
I have created that parent window by the following code:
settingsFrm settingsWindow(this, "Settings");
settingsWindow.ShowModal();
I have tried to use FindWindowByName to get the value of the first text ctrl as follow:
wxLogMessage(dynamic_cast<wxTextCtrl*>(settingsWindow->FindWindowByName("keywords_txt"))->GetValue());
But unfortunately, it doesn't work and gives me a runtime error.
I don't know if that method suitable to do what I want.
How to get the value/other of a control through its parent window?
From your comments, it seems like you expect the function to find the control from the name of the variable in your code which is not how it works and would be pretty much impossible.
FindWindowByName() uses the window name (and, as a fallback, label, but this is irrelevant here because text controls don't have labels anyhow), so for it to work you need to set the window name when creating the control, using the corresponding parameter of its ctor. This is very rarely useful in C++ code, however, as it's simpler to just store a pointer to the control in some variable and use this instead.
FindWindowByName() can often be useful when creating controls from XRC, however. If you do this, then you should specify names for your controls in XRC and pass the same name to this function.
How did you create the TextCtrl instance? You should have something like wxTextCtrl m_textCtrl1 = new wxTextCtrl(/// arguments); Accessing the value should be very easy, as wxString text = m_textCtrl1->GetValue(); You definitely don't need FindWindowByName just for what you are trying to do here.

Display the input of QLineEdit in a different window and or dialog?

I am writing a small QT gui application where there is a QLineEdit in my mainwindow.ui and I want to display the entered text in a separate dialog and or window when a button is pressed.
Now, I have stored the input in a variable, and I am also able to show this string on a label within this same mainwindow,
void MainWindow::on_GoButton_clicked()
{
QString mytext = ui->lineEdit_1->text();
ui->label_1->setText(mytext);
}
Now, I want to open a popup dialog (can be a window also), for example SecDialog;
SecDialog secdialog;
secdialog.setModal(true);
secdialog.exec();
and display the text of mainwindow->mytext string variable in a label of the SecDialog. How can I do that ??? I know it is a basic level question, but I think it will help clear lot of my doubts reagrding moving values of variables in between forms and classes.
Situation
So this is your situation:
From your code, the dialog is a modal dialog:
SecDialog secdialog;
//secdialog.setModal(true); // It's not needed since you already called exec(), and the
// dialog will be automatically set to be modal just like what
// document says in Chernobyl's answer
secdialog.exec();
Solution
To make the dialog display the text from the Window,
the concept is to pass the information(text) from the Window
to the dialog, and use a setter function from the dialog to display it.
Like Floris Velleman's answer, he passed the mytext string (by reference) to a customized dialog constructor and called the setter theStringInThisClass(myString) at once.
The implementation detail of this function is complemented by Chernobyl's answer (use the name setLabelText instead):
void SecDialog::setLabelText(QString str)
{
ui->label->setText(str); // this "ui" is the UI namespace of the dialog itself.
// If you create the dialog by designer, it's from dialog.ui
// Do not confuse with the ui from mainwindow.ui
}
Chernobyl suggested another way which calls the setter in the slot function and it bypasses the need of defining another constructor, but basically the concept is the same:
void MainWindow::on_GoButton_clicked()
{
QString mytext = ui->lineEdit_1->text();
ui->label_1->setText(mytext);
SecDialog secdialog;
secdialog.setLabelText(myText); // display the text in dialog
secdialog.exec();
}
Comment
I try to illustrate the concept as clear as possible, because from my previous experience on your question, you just "copy & paste" codes from answers and took them as your final solution, which is not right. So I hope this summary could help you understand the concept and then you may write your own code.
This task can be easy done with getter/setter method or with signal and slot, but setter is more suitable here. In SecDialog header:
public:
void setLabelText(QString str);
//in cpp
void SecDialog::setLabelText(QString str)
{
ui->label->setText(str);//it is label dialog
}
Usage:
secDialog.setLabelText(myText);
Also line where you set modal to true is not necessary because
This property holds whether show() should pop up the dialog as modal
or modeless. By default, this property is false and show() pops up the
dialog as modeless. Setting his property to true is equivalent to
setting QWidget::windowModality to Qt::ApplicationModal. exec()
ignores the value of this property and always pops up the dialog as
modal.
Assuming SecDialog is a custom class with an interface file as well you might want to pass it as a constructor argument or pass it by using another function.
So in the SecDialog constructor you could have something like:
SecDialog::SecDialog(QWidget* parent, const QString& myString)
: QDialog(parent),
theStringInThisClass(myString)
{}
And then you could call it like:
SecDialog secdialog(this, mytext);

Taking data from a Dialog in Qt and using it in a Ui

So I'm making a text editor using Qt and right now I have a button that opens a dialog called "Format text". I want it to work kind of like the dialog in notepad called "font" where you select a few text attributes from some drop down lists and it shows you what your text will look like. Right now I have it working where you can select the font style, font color, and font size and hit preview and it shows you in a box in the dialog what your text will look like. However, I have a button called "okay" which is supposed to change the highlighted text or the text you are about to type, but I can't figure out how to display those changes on the main window. The .ui files are private and a lot of the already made functions and pointers are the same in every ui file so if I change the ui file to pubic I have to change a whole bunch of things. Can anyway give me a simple answer? I'm trying to do this with as little confusion as possible. More coding and less confusion is better than less coding and more confusion for someone of my skill level. Sorry that this is all one giant paragraph and that I didn't provide any code, but I didn't think the code was necessary, however if you do need some of the code i'd be happy to share it.
Thank you for your help and your time. I hope you all have a nice evening.
QDialog have a signal called finished(), you can connect this signal with your slot. To accomplish your work, pass a QSettings or for simplicity QStringList to dialog settings (responsible for changing font, color ...), the QStringList will save user defined settings, after closing the dialog, iterate through QStringList member to alert Main window.
A pseudo code will look like this
Class Editor:
Editor::Editor()
{
TextSettings textSettings;
textSettings.setSettings(settings); // settings is a member
connect(textSettings, &finished(int)), this, SLOT(alertEditor(int)))
}
Editor::alertEditor(int s)
{
if(s == 0)
{
for (int i = 0; i < settings.size(); ++i)
settings.at(i).toLocal8Bit().constData(); // extract various user settings
}
}
Class TextSettings:
TextSettings::TextSettings(QStringList settings)
{
settings << ui->combobox->currentItem(); // font name as example
}

An "About" message box for a GUI with Qt

QMessageBox::about( this, "About Application",
"<h4>Application is a one-paragraph blurb</h4>\n\n"
"Copyright 1991-2003 Such-and-such. "
"For technical support, call 1234-56789 or see\n"
"http://www.such-and-such.com" );
This code is creating the About message box which I wanted to have with two exceptions:
1) I would like to change the icon in the message box with an aaa.png file
2) And I would like to have the link clickable. It looks like hyperlink (it is blue and underlined) but mouse click does not work
Any ideas?
I think you should create a custom QWidget for your about widget. By this way, you can put on the widget all you want. By example, you can place QLabel using the openExternalLinks property for clickable link.
To display a custom image on the QWidget, this example may help.
For the icon, you need to just set the application icon. Something like this:
QApplication::setWindowIcon(QIcon(":/aaa.png")); // from a resource file
As for making the links clickable, I don't think it can be done with the QMessageBox::about API directly.
QMessageBox msgBox;
msgBox.setTextFormat(Qt::RichText); // this does the magic trick and allows you to click the link
msgBox.setText("Text<br />http://www.such-and-such.com");
msgBox.setIcon(yourIcon);
msgBox.exec();
For future reference, the docs state that the default type for textFormat is Qt::AutoText. The docs further state that Qt::AutoText is interpreted as Qt::RichText if Qt::mightBeRichText() returns true, otherwise as Qt::PlainText. Finally, mightBeRichText uses a fast and therefore simple heuristic. It mainly checks whether there is something that looks like a tag before the first line break. So, since you dont have a tag in your first line, it assumes that it is plain text. Set it to RichText explicitely with msgBox.setTextFormat(Qt::RichText); to make it act accordingly.
there's a message in the qtcenter about it:
http://www.qtcentre.org/threads/17365-Clickable-URL-in-QMessageBox
Use http://doc.qt.nokia.com/latest/qlabel.html#setOpenExternalLinks
main.cpp
QApplication app(argc, argv);
app.setWindowIcon(QIcon(":/images/your_icon.png"));
mainwindow.cpp (into your slot if you have one)
void MainWindow::on_aboutAction_triggered()
{
QMessageBox::about(0, "window title", "<a href='http://www.jeffersonpalheta.com'>jeffersonpalheta.com</a>");
}

Handling a QTextEdit from another form

So the question is how do I set a value of textEdit from another form?
You have two options, either you can simply call one of the setText functions from a function within another form like this:
otherForm->setPlainText(text);
Or you could connect the two forms with signals like this:
connect(form1, SIGNAL(updateText(const QString&)),
form2->myTextEdit, SLOT(setText(const QString&)))
Either of these are valid ways to do it.
myTextEdit->setPlainText(text), myTextEdit->setHtml(text) or by directly modifying the editor's QTextDocument instance.