How to Change QMessageBox Icon and Title - c++

I am creating a ui app with Qt c++.
I have a error message that I have created by using QMessageBox Class like :
QMessageBox errorMessage;
errorMessage.critical(0, "Error", "An error has occured !");
errorMessage.setFixedSize(500, 200);
It is like:
And I want to change the red circled things which are the icon and the title.
I would appreciate any help.
Thanks in advance.

However you can use of QMessageBox::critical(0, "Error", "An error has occured !"); because critical(...) method is static and theres no need for create an instance of QMessageBox.
Use bellow code :
QMessageBox mb("Application Name",
"Hardware failure.\n\nDisk error detected\nDo you want to stop?",
QMessageBox::NoIcon,
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::NoButton,
QMessageBox::NoButton);
QPixmap exportSuccess("/media/msi/Information/Pictures/Icons/Icons Pack/PNG/48X48/about.png");
mb.setIconPixmap(exportSuccess);
mb.exec();
This example work 100%

If you want to set the icon for every window you can do so globally with this:
QApplication::setWindowIcon(QIcon(":/GuiMain/gh_resource/GH Icon.ico"));
You also aren't required to use pixmap in my experience, you can use:
mb.setWindowIcon(QIcon(":/path/to/icon.ico"));
This will work on individual message boxes, just add your custom icon to your .qrc file

Simply you can set Icon for your Application it will automatically set on your MessegeBox

Related

Making a simple message box using C++/WinRT

I'm having trouble making a simple message dialog in C++/WinRT. Something as simple as "You clicked this: press ok to continue" nothing fancy. In the standard Windows API you can simply write MessageBox() and new popup will show up where you can click ok, and you can do somthing similiar in C++/CX with
auto messageDialog = ref new Windows::UI::Popups::MessageDialog("You clicked on the button!"); messageDialog->ShowAsync();
I've tried using IMessageDialog and ContentDialog classes with no success and can't seem to find a single example out there on how to do it without getting into writing Xaml code which for my perticular project seems unnecisary for something as simple as a message box. Maybe I'm just not setting them up right? But then again there are no examples on how to set it up in the first place (in C++). What am I missing?
Thanks!
I was able to form a simple message dialog using this:
winrt::hstring Title = L"Hello";
winrt::hstring Content = L"Some cool content!";
winrt::Windows::UI::Popups::MessageDialog dialog(Content, Title);
dialog.ShowAsync();
Make sure to also include <winrt/Windows.UI.Core.h> so you can get access to the UI library.
And here is the result:

QMessageBox disappears

I'm trying to display a QMessageBox through calling its show function in a lambda function like the following:
connect(ui->graphButton, &QAbstractButton::clicked, [](void){
QMessageBox message;
message.setText(tr("Graphing button clicked"));
message.show();
});
However, upon clicking the graphButton, the QMessageBox window appears only momentarily before disappearing. How can I fix this?
message.exec();
to execute it.
Full code :
connect(ui->graphButton, &QAbstractButton::clicked, [](void){
QMessageBox message;
message.setText(tr("Graphing button clicked"));
message.exec();
});
show only set the visibility status of the widget : http://doc.qt.io/qt-4.8/qwidget.html#show http://doc.qt.io/qt-4.8/qwidget.html#visible-prop
while exec has the desired behavior http://doc.qt.io/qt-5/qmessagebox.html#exec
You do not need to use show, because it is the default visibility. Use it when you disabled the visibility of a widget to enable it again.
Use QMessageBox.setModal to set the message box to "Modal" mode (that is, it blocks the execution until it is done working) and then use "open" method to show the messagebox.
connect(ui->graphButton, &QAbstractButton::clicked, [](void){
QMessageBox message;
message.setText(tr("Graphing button clicked"));
message.setModal(true);
message.open( ... );
});

Custom GtkFileChooserWidget

I have some troubles with bulding custom GtkFileChooserDialog.
How can I set custom icons to popup dialogs (for example, this dialog popups, when I click to CD-ROM with not insterted CD) and set custom name to "Name", "Modified", "Size" labels?
Thanks!
P.S. Please, sorry for my English...
The filenames and modified date is displayed in a treeview inside GtkFileChooserDialog.
I dont think there is a way to get the treeview from the GtkFileChooserDialog.
If you manage to get the treeview. You can change the column names of the treeview.
Refer Programming in GTK+ by andrew krauss, building your own custom widgets module.
You might get some help.
This is the structure of GtkFileChooserDialog,
struct GtkFileChooserDialog
{
GtkDialog parent_instance;
GtkFileChooserDialogPrivate *priv;
};
/* GtkFileChooserDialog private */
struct _GtkFileChooserDialogPrivate
{
GtkWidget *widget;
char *file_system;
/* for use with GtkFileChooserEmbed */
gboolean response_requested;
};
There is no specific API in GTK to get the treeview from GtkFileChooserDialog.
Treeview is not part of this structure, It is internal and not available for programmer to access.
If this doesnt work then, try writing your own custom widget. or design your own filechooser dialog.
I am not able to understand clearly, what is your question.
This is what i understood from your question, you want to change the button names in the file chooser dialog.
You can specify the names of the buttons while creating the file choose dialog.
GtkWidget *dialog;
dialog = gtk_file_chooser_dialog_new ("Open File",
parent_window,
"Buttonname 1",
"Buttonname 2",
"Buttonname 3",
NULL);
Buttonname 1, 2 ,3 are the names of your custom buttons.

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>");
}