I have some trouble trying to set an icon for my QT application.
The icon is named "room.ico" and is on the same directory as the source file.
Here is the code :
#include <QApplication>
#include <QWidget>
int main( int argc, char *argv[ ] )
{
QApplication app( argc, argv) ;
QWidget fenetre;
fenetre.setWindowIcon(QIcon("room.ico")); // Nothing happens
fenetre.setWindowTitle("Heloo");
fenetre.show();
return app.exec() ;
}
I have tried to add win32:RC_ICONS += room.ico in the .pro file but that didn't work. I have also tried "./room.ico" but still no icon.
I have tried to use this :
QPixmap pixmap = QPixmap ("room.ico");
fenetre.setWindowIcon(QIcon(pixmap));
And guess what !!! it didn't work ... i'm just a newbie to QT :p
Any suggestions will be appreciated , thanks
QT's documentation for QWindow::setWindowIcon should be what you need.
Make an icon file (you appear to have done this already: room.ico
Add your icon file to a QT resource file (.qrc or .rc) which you should add to your project (the documentation discusses how to do this
Use setWindowIcon and pass in a QIcon:
app.setWindowIcon(QIcon(":/room.ico")); (this assumes your file is in the resource file)
Your problem appears to be that you didn't prepend :/ when passing in the filename to QIcon.
Related
Having trouble adding background image on the widget, even though I referenced the recent codes online.
This is my current code for main.cpp:
#include <QApplication>
#include <QWidget>
int main (int argc, char **argv){
QApplication app (argc,argv);
QWidget *w=new QWidget();
w->setStyleSheet("background-image: url(:/cover.jpg);");
w->setWindowTitle("Test");
w->show();
return app.exec();
}
After executing the code, how come the widget remains blank? Thanks in advance!
QtCreator is an IDE designed by Qt. It's just an interface.
I checked your implementation and I don't see anything wrong. It also work well on my pc. Can you check your image url or try with another image ?
Btw, if you're on linux, try removing : character after url ;
w->setStyleSheet("background-image: url(/cover.jpg);");
EDİT:
If jpg is in the same directory with your application, it should be ;
w->setStyleSheet("background-image: url(./cover.jpg);");
You can give a full path to avoid this kind of errors.
"Is it possible to add background image on QWidget without using QtCreator?"
Yes, of course it is.
QtCreator is just an IDE. You don't need to use it at all to write code using the Qt library.
Just as you can use it to write code that does not use Qt at all.
I receive this error (title, below) whenever I try to run the following code:
#include <QCoreApplication>
#include <QQuickView>
int main(int argc, char *argv[]){
QCoreApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl::fromLocalFile("app.qml"));
QObject *object = (QObject*)view.rootObject();
view.show();
delete object;
return app.exec();
}
Cannot create window: no screens available
The program has unexpectedly finished.
All I can find online for that error are bug reports arising from specific conditions significantly more involved than the above.
app.qml is a file that runs fine alone, i.e. without the above C++ and in a separate project configured as a 'Qt Quick UI'. Giving it's qrc:// path, or deliberately specifying a file which does not exist has no effect.
Note the QObject* cast - this was not present in the docs, but without it:
/main.cpp:11: error: cannot initialize a variable of type 'QObject *' with an rvalue of type 'QQuickItem *'
How should this be done?
The QCoreApplication can be used with console application, not with GUI ones, i.e. you have to use a QGuiApplication object. It seems to me that you created a console application instead of a graphical one.
You can create a proper application via the Qt Quick Application, add your "app.qml" as a resource to that project and call such a file instead of the default "main.qml", provided by the project template.
If you want to quick fix your current project, just check that the .pro file is set to import GUI libraries:
QT += gui qml quick
Set your qml file as a resource:
Create a new resource file via file -> new File or Project... -> Qt -> Qt Resource File
Right click the newly created .qrc file and click add existing file to add your "app.qml" file
Finally, rewrite your main like this:
#include <QQuickView>
#include <QGuiApplication>
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv); // GUI APPLICATION!!!
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:///app.qml")));
view.show();
return a.exec();
}
However, going for the Qt Quick Application project would be the wiser choice.
I'm working on an QT HTML5 application and I was wondering how i could add an top menu just like normal program ( with the default file, tools, help... options ).
I think that I've to change something in the html5applicationviewer.cpp, but I've got 0 knowledge of that ( I'm learning this... )
Even if you could point me a little bit in the right direction, I'm grateful. I've searched around, but found absolutely nothing about this topic ( but maybe i didn't search right... )
If you need more info, please ask.
Simplest way to add normal "desktop"-style menus to a Qt app is to use QMainWindow which has good support for menus.
Here's something to get you started. First I created the default HTML5 Qt application with Qt Creator (SDK version 5.2.1). Then I edited the main.cpp and added some lines. Result is below, original lines without comment and all added lines with comment.
#include <QApplication>
#include <QMainWindow> // added
#include <QMenuBar> // added
#include <QMenu> // added
#include <QAction> // added
#include "html5applicationviewer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow w; // important, viewer is in stack so w must be before it!
Html5ApplicationViewer viewer;
w.setCentralWidget(&viewer); // set viewer as the central widget
QMenu *fileMenu = w.menuBar()->addMenu("&File"); // create file menu
QAction *exitAction = fileMenu->addAction("&Exit"); // create exit action
QObject::connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); // make the action do something
viewer.setOrientation(Html5ApplicationViewer::ScreenOrientationAuto);
//viewer.showExpanded(); // will be shown by main window
viewer.loadFile(QLatin1String("html/index.html"));
w.show(); // show main window
return app.exec();
}
I'm trying to load in a CSS file for formatting across my entire Qt application. Currently I have my "stylesheet.css" file in the same folder as my built exe (both debug and release). However, upon running the program it produces no errors and simply outputs "test: ", so it's clearly not finding the file or perhaps I'm not reading it properly?
Forgive me if it's a dumb mistake - I'm fairly new to both Qt and C++.
#include "mainwindow.h"
#include "qfile.h"
#include "qtextstream.h"
#include <QApplication>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication program(argc, argv);
QFile styleFile("stylesheet.css");
styleFile.open(QIODevice::ReadOnly);
QTextStream textStream(&styleFile);
QString styleSheet = textStream.readAll();
styleFile.close();
program.setStyleSheet(styleSheet);
std::cout << "test: " << styleSheet.toStdString() << std::endl;
MainWindow w;
w.showMaximized();
return program.exec();
}
After digging a bit deeper, it turns out I was lacking a QRC file (which I wasn't even aware existed). So I created a resource (QRC) file, added the prefix "/style", and then added my stylesheet to that prefix. It now works flawlessly. Also, at one point I changed the .css to a .qss (thought I should mention it, although I doubt it made any difference).
Here's the final code:
#include "mainwindow.h"
#include "qfile.h"
#include "qtextstream.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication program(argc, argv);
QFile styleFile(":/style/stylesheet.qss");
if(styleFile.open(QIODevice::ReadOnly))
{
QTextStream textStream(&styleFile);
QString styleSheet = textStream.readAll();
styleFile.close();
program.setStyleSheet(styleSheet);
}
MainWindow w;
w.showMaximized();
return program.exec();
}
Change
QFile styleFile("stylesheet.css");
to
QFile styleFile("C:/path/to/stylesheet.css");
You need to pass the full file path for your program to find the file. If you just give its name but not the directory it's in, the program will search for it in the current directory only (which is not necessarily the directory of your exe file), and if it's not there, it won't find it.
If you want to deploy this CSS file with your application, you are looking for the Qt resource file support.
its coz u shoud uncheck " shadow build " under projects. and check the path of the project . and make sure that .css file is inside the project folder
Noob: How to display an image
I am very new to this, just starting actually. I need to figure out how to display an image on screen.
First I tried:
Source code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QPixmap qp = QPixmap("../images/tank2.bmp");
if(qp.isNull())
{
printf("Yes its null\n");
}
else
{
QGraphicsPixmapItem item(QPixmap("../images/tank2.bmp"));
scene.addItem(&item);
}
view.show();
return a.exec();
}
from:
Qt jpg image display
it compiles and runs but doesn't show an image. returns 0, etc.
Then I just sorta messed around from there. I'm also curious about something: In Qt editor they show a file structure that doesn't exist on the disk. They have files "Headers", "Sources", and Resources whereas on the systems its just a folder "projectname" with all the files in one folder. Is this just for visual clarity?
The version of QtCreator I'm using is 2.4.1 running Qt 4.7.4 64 bit.
My eventual goal is to make a widget where a picture is a clickable icon where you select that pic and can place it on a larger screen like a tile.
Another Question: Why does Qt have things like "QString" and "QChar"? Is there something wrong with the normal c++ libraries?
If you just want to display a simple image then make a Qlabel the central widget and call setPixmap() passing it your image path
"Another Question: Why does Qt have things like "QString" and "QChar"?
Is there something wrong with the normal c++ libraries?"
Yes there is lots wrong with the normal libraries - at least for std::string.
When Qt was started there wasn't very good cross platform STL support and the standard libraries were very bad at Unicode and support for translations. QString doesthis very well - although I think a combination of modern STL and boost can probably do everything QString can do.
Almost all of the Qt types are automatically reference counted so you can pretty much ignore memory management for them AND pass them around freely. There are also some tricks Qt can do because the extra MOC compile pass means it has Java-like introspection that standard C++ doesn't.
But in general you are free to use standard C++ types (Qt: Qt classes vs. standard C++)
Tested like this, it works. Don't forget to create qrc file.
#include <QtGui/QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include "mainwindow.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QPixmap qp = QPixmap(":/images/123.bmp");
if(qp.isNull())
{
printf("Yes its null\n");
}
else
{
printf("HAHA");
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap(":/images/123.bmp"));
scene.addItem(item);
}
view.show();
return a.exec();
}
Here are qrc file:
<RCC>
<qresource prefix="/">
<file>images/123.bmp</file>
</qresource>
</RCC>
And .pro file
QT += core gui
TARGET = testimage
TEMPLATE = app
SOURCES += main.cpp
RESOURCES += 123.qrc
I think your problem is here:
{
QGraphicsPixmapItem item(QPixmap("../images/tank2.bmp"));
scene.addItem(&item);
}
item goes out of scope before you'll actually use it.
I'm also pretty sure you meant that to use the QPixmap that you loaded earlier at the top-level scope.
Generally speaking, you want to limit your questions on SO to a single question... but to address your last question: QChar and QString allow the Qt libs to make several assumptions about strings. The most obvious of which is that QStrings have a standardized encoding.