How to apply an image to a QPushButton in QT Creator - c++

I can't manage to view any picture on Qobjects, like a QPushButton, using the setIcon() function.
This probably has to do with the image url being erroneously specified, inside the QPixMap pix("url") function. This is what I've got:
QPixmap pix(":/Resources/flag.png");
QIcon icon(pix);
button->setIcon(icon);
I have a flag.png file inside the directory "Other files/Resources/flag.png".
I really don't know how resources should be placed.

You have to create a Qt Resource file (File > New > Qt > Resource File). Then add a prefix into the resource file, then add a file to the resource file.
After all of that is done, right click on the listing for that file and select Copy resource path. Then paste that path into your cpp file, and surround it in quotes.
Hope that helps.

Related

Qt Image won't show up in button

I am creating a Qt application and I have an image that I want to use for a button instead of text. Unfortunately all that shows up is an empty button.
I've tried two different methods to get it to show up with the same results for both methods.
Code for Method 1:
ui->setupUi(this);
QPixmap pix(":/svg/resources/menu.svg");
int w = ui->menuButton->width();
int h = ui->menuButton->height();
ui->menuButton->setMask(pix.scaled(w,h,Qt::KeepAspectRatio).mask());
I found the info for the second method here: Adding image to QPushButton on Qt
Code for Method 2:
ui->setupUi(this);
QIcon icon(":/svg/resources/menu.svg");
ui->menuButton->setIcon(icon);
Could someone please help me figure out why my image isn't showing up and the button is just empty?
In my project using .svg images as button icons are no problem, maybe the button size is missing, try:
ui->menuButton->setIcon(QIcon(":/svg/resources/menu.svg"));
ui->menuButton->setToolTip("optional tooltip");
ui->menuButton->setFixedSize(QSize(28,28));
Assuming you stored your icons correctly in a resource file. If not, create a new:
right click on your top project folder (in the project-tree) -> Add new.. -> choose Qt on the left an Qt Resource File on the right window -> a new Window apears.
Add Prefix -> Add Files (your icon)
You use .svg image format. Are you sure your application load image format plugin for .svg? Image plugins must be in directory "imageformats" in current directory of your application. Avaliable plugins you can find in Qt directory .../Desktop/Qt/<version>/<mingw or msvc>/plugins/imageformats

QFileDialog: add suffix after selecting file

I need to add suffix to selected filename in QFileDialog with QFileDialog::AcceptSave accept mode. For example, after selecting "1.txt" file in QFileDialog edit should be select "1_suffix.txt". It should be added before file accepting, because I need the user to have the ability to change the filename before applying file.
code:
m_dialog.setAcceptMode(QFileDialog::AcceptSave);
m_dialog.setWindowModality(Qt::WindowModal);
m_dialog.setFileMode(QFileDialog::AnyFile);
m_dialog.setDefaultSuffix("_suffix");
if(m_dialog.exec() == QFileDialog::Accept)
{
setPath(m_dialog.selectedFiles()[0]);
}
Usually, a QFileDialog is displaying the platform file dialog. To get the behavior you want, you'd need to use platform-specific mechanisms; Qt doesn't implement such functionality.
If you're using the non-native file dialog, you could inspect its structure to find the widget(s) you're after, filter relevant events on them, and inject the behavior you need.
Try extending QFileDialog and subscribe to QFileDialog signals
void fileSelected(QString file)
void currentChanged(QString path)
It can be a start.

QFile: directory not found

I need to write a console application that takes a file, it opens it, and then it calls another procedure based on the information inside the text file.
The only problem is that QFile::errorString() returns:
No such file or directory.
I have been using this implementation in all the programs I had to, and yes, the file exists at that directory.
The code is:
QFile fileName("D:/file.txt");
QString read_from_file;
if(fileName.open(QIODevice::ReadOnly)){
QTextStream in(&fileName);
while(!in.atEnd())
{
read_from_file = in.readLine();
qDebug()<<read_from_file;
}
fileName.close();
}
qDebug()<<fileName.errorString();
Make sure that the file really exists.
QFile::exists("D:/file.txt") – This will return true if the file exists.
QDir("D:/").entryList() – This will return the list of the files and directories located at the specified path; the needed file should be in the list.
As you pointed out in the comments, the problem was the hidden file extensions on Windows.
Open Folder Options by clicking the Start button, clicking Control Panel, clicking Appearance and
Personalization, and then clicking Folder Options.
Click the View tab, and then Advanced settings <...>
To show file name extensions, clear the Hide extensions for known file
types check box, and then click OK.

setting custom Icon

I am struggling with adding a custom icon to a QToolButton.
I have a resource file(properly under RESOURCES in the .pro file):
myResourceFile.qrc containing
/images
ICON_TEST.png
and code
dragButton = new QToolButton(this);
QString resourcePath = ":/images/ICON_TEST.png";
QPixmap pixIcon(resourcePath); //THE LINE THAT GIVES THE ERROR!
dragButton->setIcon(QIcon(pixIcon));
I get the error:
no match for call to QPixmap(QString &)
How do I pass the proper path to the QPixmap object and then the object to setIcon()?
edit:
Just confirmed that the .png file exists and is recognized by Qt:
File exists - true ":/images/ICON_TEST.png"
with:
qDebug()<<"File exists -"<<QFileInfo(":/images/ICON_TEST.png").exists()<<" "<<
QFileInfo(":/images/ICON_TEST.png").absoluteFilePath();
EDIT WITH SOLUTION: below as answer
Question remains, why this happened? The variable pixIcon is declared in the .h file of the class as:
QPixmap pixIcon;
and assigned a value in the constructor:
pixIcon(resourcePath);
Which to me seems to be almost equivalent to doing it in one line.
This was on Windows Vista with Qt Creator 2.0.
For some reason, when doing everything in one line it worked as it should have:
dragButton->setIcon(QIcon(QPixmap(resourcePath)));

Failing to remove attributes of Tree View in Qt

I am a newbie in QT. I am working on a app where I need to display the FileSystem using a treeview.
Basically I have a widget in my .ui file on which I have put a Treeview. Then in my .cpp file I have written the following code:
model = new QFileSystemModel(this);
model->setRootPath(QDir::homePath());
ui->treeView->setModel(model);
In my .h file I have put the following:
QFileSystemModel *model;
When I run the app, it displays the file system inside the treeview but it also shows Name, Type, Size, DateModified above it. I want to get rid of these.
Here is the sample Image:
How can I achieve it?
I think that QTreeView::hideColumn does it.