Hyperlink with image in one QTableView cell - qt

I have a QTableView with custom model and delegate. Task is to create cell which will contain image and hyperlink. By clicking on image user will copy hyperlink to clipboard, and by clicking of hyperlink user should open link in browser.
Is it a possible task?

Sure it is possible.
One of the way to solve this is to use call setIndexWidget() of view where you bypass QLabel with image or hyperlink or both using html (rtf).
Another way is to have own subclassed QItemDelegate in which you can reimplement createEditor() and set persistent editor with openPersistenEditor().

Related

How to set different icons to different buttons in Qt?

I have a lot of buttons that I want to set an icon to. Moreover, this icon is not the same for each button.
I do this just for one of them:
QString str=(qApp->applicationDirPath());
str.append("/pic/kb.png");
QPixmap pixmap(str);
QIcon ButtonIcon(pixmap);
ui->btnShowKB->setIcon(ButtonIcon);
ui->btnShowKB->setIconSize(pixmap.rect().size());
but I really have a lot of button (btn1,btn2,btn3,....,btn9).
How can I set other images for other buttons (/pic/1.png , /pic/2.png , /pic/3.png , .... , /pic/9.png)?
Do I have to create a new QPixmap for each one, or is there a simpler solution?
First of all, if you're using designer, so use it fully, not only for adding widgets. You can add icon to your buttons from there. Just add resource file to your project, load images to it and then choose needed to buttons. Or, if you don't want use resource file, you can upload images from any directory.
The way you are doing it, the only things that change in your code is obviously the name of the file and the button you want to set an icon to. So you should create a method taking a QString and a button as parameters, and call it whenever you need it for your desired button. (In the below code, I use a QPushButton as a button, maybe it is different for you so change it accordingly).
void yourClass::setButtonIcon(QString iconPath, QPushButton* button)
{
qApp->applicationDirPath().append(iconPath);
QPixmap pixmap(str);
QIcon buttonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());
}

PyQt how to add link to QTextBrowser

I don't know why I'm having such a hard time finding this, but how do you add a hyperlink to QTextEdit or QTextBrowser? I'm trying to make something that has a list of links and when you click on a link you get the object that the link is referencing.
You need to set the acceptRichText property to true. Then you can just pass in HTML using the setHTML() slot.
win.setHTML("<a href="http://foo>Bar</a>");

Qt4: QLabel -> QPixmap -> click to open URL

I have a QLabel without any text but with a QPixmap image. I can not figure out a way to open a url when the user clicks the image. I can not use text in QLabel here.
You don't need to put text in, but you do need to switch to either a subclassed label or to use a QPushButton instead. If you use a QPushButton (which is the easiest) then you can change the relief layout so it looks flat again (since the default button doesn't).
You could install an event filter on the label and filter for mouse press events. For an example, see my answer to a similar question.

How to make links clickable in a QTextEdit?

Is there a way to make links clickable in a QTextEdit?
I know I can use a QTextBrowser and connect to anchorClicked but I'd rather keep the editing and viewing all in one widget, and have clickable links when I set the widget to read-only mode.
Is this possible or am I stuck with having two separate widgets in a stack and switching between them?
Since QTextBrowser inherits from QTextEdit, you should use it and set the readOnly property to false. Then you can change the style of the QTextBrowser as you want.

Passing variables from one form to another in Qt

I've got two forms, the mainform which opens up a dialog box that has a text box in it. How can I pass the text from the textbox back to the mainform? I've tried alot of different methods but I think I'm missing something simple. Thanks for any help.
The dialog box still exists after it closes. So you can, from the main form, do something like this:
QString text = subform->textEdit->text();
This assumes your dialog box is subform and the name you gave to the text edit box is textEdit. Make sure you make textEdit public in the designer.
If you don't want to make textEdit public, then you can add a getter to subform.
If you use the MVC pattern, you create the model object (container for your data) and pass to the text box to fill in the text value itself. When the dialog is closed, just read the value from the model and put it wherever you need it.

Resources