QPushButton setIcon not working - c++

I have just started using Qt and I've been trying to get a single button to display an icon but for some reason I can't get it to work. This is my code
QPushButton* button = new QPushButton();
button->setIcon(QIcon("icon.png"));
button->show();
I don't know what I'm missing here, the icon.png and the .cpp file are both in the same folder.. What am I doing wrong?

Add icon folder to your resources.qrc and try this one:
setIcon(QIcon(QPixmap(":/icons/yourIcon")));
It works in my project

Related

Can't move/rename/delete folder while some of its subfolders are expanded in QFileSystemModel

I'm developing a Qt application on Windows. To show the files of a specific folder, I'm using QTreeView together with QFileSystemModel. So far so good, but I came across a very specific issue which is driving me nuts: while I'm with a folder expanded in my application, I can't do anything with its parent folder.
I built a small project only to show this issue. Here's how I define my QFileSystemModel and apply it to my QTreeView:
QFileSystemModel *myModel = new QFileSystemModel;
myModel->setRootPath(myRootPath);
ui->treeView->setModel(myModel);
To exemplify my problem, check out this image
While I have "Test Folder 2" not expanded, I can do what I want to "Test Folder". I can rename, move, or even delete via Windows Explorer, and everything is applied to my program. However, when I expand "Test Folder 2", suddenly my "Test Folder" is not editable anymore. Windows says the folder is "open in another application".
I believe anyone can reproduce this issue with the three lines above, so I don't think it's a project specific problem. Does anyone knows why this is happening?
EDIT: Apparently this is a windows only problem. Just tried on linux and it worked just fine. Is this a NTFS problem? Any ideas?
You should try to set the read only property.
#include <QApplication>
#include <QTreeView>
#include <QFileSystemModel>
int main(int argc, char** args) {
QApplication app(argc, args);
auto myModel = new QFileSystemModel;
myModel->setReadOnly(true);
auto treeView = new QTreeView;
myModel->setRootPath("C:/Temp/A");
treeView->setModel(myModel);
treeView->show();
app.exec();
}

how to integrate qml file into qt-widgets app?

I have an qt widgets application. I want to add the switch control to my form.
I added to my form QDeclarative widget and add this line to the code but nothing shown.
ui->declarativeView->setSource(QUrl::fromLocalFile("test.qml"));
This is the content of the qml file (I added this file to the resources)
It display in qtcreator under resources/[project_name.qrc]/test.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
Button{
text:aaaa
}
I added the pro file : qt += declarative
What am i doing wrong??
I'm using Qt 5.4.1 QtCreator 3.3.1.
1. QDeclarativeView is for older Qt versions. If you are porting an application to Qt 5 then you can refer this documentation.
2. For your application you can use the new class in Qt 5.x QuickView as shown below.
Create a layout in your ui. Or do it via code. Then add the view to the layout as shown below:
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setMinimumSize(300, 200);
container->setMaximumSize(300, 200);
view->setSource(QUrl("qrc:/test.qml")); // Fetch this url by right clicking on your resource file.
ui->verticalLayout->addWidget(container);
3. In the .pro file just add quick module:
+quick
4. Reference: Introducing QWidget::createWindowContainer()
5. Note: If you have to add a url to a resource file you have to use :/ refer here for more details.

qtcreator tray icon under window does not show up when deployed

i'am trying to deploy a QT application made in QT Creator which goes back to system tray when closed.
I have made a svg tray icon, when i run it from QT Creator, either in debug and in release mode under Window 7, the tray icon shows up, but when i copy everything to another directory to make a distributable archive from it, the tray icon does not show up anymore.
Of course i search already for solutions, but everything i have found yet, i have made.
So what i have:
trayicon.svg file in project root
qrc file created, adding trayicon.svg into the resource files root
in project .pro file: RESOURCES += resources.qrc
copied binary + necessary dll's to target directory
copied QT plugins imageformats/* to target dir imageformats
added QApplication a(argc, argv); a.addLibraryPath(a.applicationDirPath()); to main.cpp
that is everything i found so far, but still the system tray icon is not showing up
What am i missing?
(current qt 4.8 + current qtcreator by the way)
#netrom
code in MainWindow : QMainWindow constructor:
trayIcon = new QSystemTrayIcon(this);
showAction = new QAction(tr("&Show"), this);
connect(showAction, SIGNAL(triggered()), this, SLOT(show()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(showAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
trayIcon->setIcon(QIcon(":trayicon.svg"));
trayIcon->show();
Create iconegines directory in your app directory (for example: c:\MyApp\iconengines).
Copy qsvgicon.dll to this new directory (for example: c:\MyApp\iconengines\qsvgicon.dll) from qt plugins directory (in my case it's c:\qt\5.4\mingw491_32\plugins\iconengines\qsvgicon.dll).
Copy QtSvg.dll to your app directory (for example: c:\MyApp\Qt5Svg.dll) from Qt bin directory (in my case it's c:\qt\5.4\mingw491_32\bin\Qt5Svg.dll).
P.S. I know I'm late, this answer is for those who will google same problem.
Again, way later for DuckDuckGo-ers:
If your icon is not in .svg you might need another solution. For my .ico system tray icon I had to copy <path-to-qt>\plugins\imageformats into the application folder. Actually only qico.dll was needed in that folder in my case, but you get my drift.

Displaying an image in QLabel

I have created a Qt interface, add a Qlabel and set pixmap to an image. imagelabel =
new QLabel(centralwidget);
imagelabel->setObjectName(QString::fromUtf8("imagelabel"));
imagelabel->setGeometry(QRect(20, 10, 371, 311));
imagelabel->setPixmap(QPixmap(QString::fromUtf8(":/liqi/kinect.png")));
this is the code displayed in ui_mainwindow.h
when i preview it using the Qtdesigner, the image can be displayed. But when i run using codeblocks, everything like buttons etc is fine but the image does not appear. Do i need to add anything into the section below ?
MainWindow::MainWindow(Tqt_interface* tqt, QWidget *parent ) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
":/liqi/kinect.png" ':' means you are trying to open image that is included as resource. Make sure u have .qrc file and it is added in .pro project file. Like:
RESOURCES += res.qrc
Also make sure that image is in the .qrc file.

How to set application icon in a Qt-based project?

How do you set application icon for application made using Qt? Is there some easy way? It's a qmake-based project.
For Qt 5, this process is automated by qmake. Just add the following to the project file:
win32:RC_ICONS += your_icon.ico
The automated resource file generation also uses the values of the following qmake variables: VERSION, QMAKE_TARGET_COMPANY, QMAKE_TARGET_DESCRIPTION, QMAKE_TARGET_COPYRIGHT, QMAKE_TARGET_PRODUCT, RC_LANG, RC_CODEPAGE.
For Qt 4, you need to do it manually. On Windows, you need to create a .rc file and add it to your project (.pro). The RC file should look like this:
IDI_ICON1 ICON DISCARDABLE "path_to_you_icon.ico"
The .pro entry should also be win32 specific, e.g.:
win32:RC_FILE += MyApplication.rc
Verified in Linux (Qt 4.8.6) and Windows (Qt 5.6):
1) Add the icon file to your project folder;
2) In the main function call setWindowIcon() method. For example:
QApplication a(argc, argv);
a.setWindowIcon(QIcon("./images/icon.png"));
To extend Rob's answer, you can set an application icon for macOS by adding and modifying the following line onto your .pro file.
macx: ICON = <app_icon>.icns
Note that the ICON qmake variable is only meant to target macOS.
For Windows, use
RC_ICONS = <app_icon>.ico if you're attaching a .ico file
or RC_FILE = <app_icon>.rc if you want to attach your icon through a .rc file. (Be sure to add IDI_ICON1 ICON DISCARDABLE "myappico.ico" into the rc file. Indentation not mine.)
For further reading, see Setting the Application Icon.
For Qt5 on Windows, move your ico file to project folder and add this line to your .pro file:
RC_ICONS = myappico.ico
Offical link: https://doc.qt.io/qt-5/appicon.html
Now that Qt has upgraded to 5.0.1, there is new method to add an application icon. First, you need prepare a resource file, named as .qrc
1) Without Qt Designer, I assume there is a QMainWindow instance whose name is MainWin. You can use:
QIcon icon(":icon/app.icon");
MainWin.setWindowIcon(icon);
2) With Qt Designer, you can modify the property of QMainWindow. Choose icon resource from .qrc and insert it into the row of windowIcon.
The above method can be used in Qt4.7, Qt4.8.x.