Set the style of GtkLinkButtons - c++

I want to use the abilities of a GtkLinkButton, but its design disturbs me. Rather than having a blue underlined text, I'd like it to look like a normal GtkButton.
What I tried is to use this code on my button:
GtkWidget *button = gtk_link_button_new_with_label ("region panel", "Input Sources");
gtk_button_set_relief (button, GTK_RELIEF_NORMAL);
gtk_button_set_use_underline (button, FALSE);
However, instead of changing the design, it simply does nothing.

If you want it to look like a GtkButton, then just replace it with that. You are not using any abilities that are special to GtkLinkButton.
You just need to make sure your click handler callback is getting fired. You are connected to "activate-link", however this signal will not exist on a standard GtkButton
Have a look here for the available signals.
https://developer.gnome.org/gtk3/stable/GtkButton.html

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.

How do I properly set up generic QT actions for a menu constructed at run time?

I am populating a sytem tray icon menu (QMenu) from entries in an xml file which is read when my application starts up.
I am unsure of how to properly set up the SLOT end of the action:
QList<CMenuItem> menuItems = m_layout->getMenuItems();
QListIterator<CMenuItem> iter(menuItems);
while (iter.hasNext())
{
CMenuItem menuItem = iter.next();
QAction *action = new QAction(menuItem.qsTitle, this);
connect(action, SIGNAL(triggered()), this, SLOT(launchMenuItem()));
trayIconMenu->addAction(action);
}
How does my "launchMenuItem()" SLOT know which menu item was triggered? I can't make a SLOT for each menu item as I don't know how many items will exist until run time.
I can think of some ugly ways to do this, but I am looking for the RIGHT way.
What I usually do is to use QAction::setData(const QVariant&) to store whatever action ID I need. Then on slot side I retrieve ID with QAction::data() and behave accordingly.
Note that QVariant obviously accepts much more than basic int (which is what I use to identify actions), you can pass any QVariant-compatible info.
edit : oh! btw, this is somehow ugly because I make use of QObject::sender() to cast triggered action back. Sorry for that, but it works anyway.

Gtk hiding window on it's callback

I have a function that I want to hide on pressing a button. I've read that I can define multiple callbacks on a signal and they will be called in the same order as defined. So I wanted to execute two functions on button press and I wanted one of them to hide window that contains this button. I tried to do it like this:
g_signal_connect(btn_confirm, "clicked", G_CALLBACK(function_that_does_stuff), NULL);
g_signal_connect(btn_confirm, "clicked", G_CALLBACK(kill_window), add_conn_win);
...
void kill_window ( GtkWidget* wdgt, GtkWidget* win )
{
gtk_widget_hide_all(win);
}
But this isn't working. Window's still there. Can someone tell me how to do that?
First things first, see what the documentation says about gtk_widget_hide_all():
gtk_widget_hide_all has been deprecated since version 2.24 and should not be used in newly-written code
Instead you should use gtk_widget_hide (). The specification says that it
Reverses the effects of gtk_widget_show(), causing the widget to be hidden (invisible to the user).
As a comment said, make sure your callbacks are being triggered. Then try
gtk_widget_hide (win);
I think that should do the work, let me know if it helps!

Making QLabel behave like a hyperlink

how can I make a QLabel to behave like a link? What I mean is that I'd like to be able to click on it and then this would invoke some command on it.
QLabel does this already.
Sample code:
myLabel->setText("Click Here!");
myLabel->setTextFormat(Qt::RichText);
myLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
myLabel->setOpenExternalLinks(true);
The answer from cmannnett85 is fine if you just want to open a URL when the link is clicked, and you are OK with embedding that URL in the text field of the label. If you want to do something slightly custom, do this:
QLabel * myLabel = new QLabel();
myLabel->setName("myLabel");
myLabel->setText("text");
myLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
Then you can connect the linkActivated signal of the label to a slot, and do whatever you want in that slot. (This answer assumes you have basic familiarity with Qt's signals and slots.)
The slot might look something like this:
void MainWindow::on_myLabel_linkActivated(const QString & link)
{
QDesktopServices::openUrl(QUrl("http://www.example.com/"));
}

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