Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Sorunome on June 04, 2013, 10:43:36 pm

Title: [QT] Desktop Derp
Post by: Sorunome on June 04, 2013, 10:43:36 pm
Inspired by jujus Browser Derp I made a Desktop Derp in C++ using QT! So far all it does is moving randomly around (with gif changing).
I'll have to come up with some good rules for which gif comes when (or make somebody else do that for me :P)
code upload soon!
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 06, 2013, 07:57:20 pm
Here we go (as promised): https://github.com/Sorunome/DesktopDerp

EDIT: I'm having trouble with a systemtrayicon and a rightclick-menu, if anybody wants to help me :D
Title: Re: [QT] Desktop Derp
Post by: Streetwalrus on June 09, 2013, 11:53:30 am
Warning ! |\|00|3 question incoming ! 3... 2... 1...
How do I compile it ? I tried g++ main.cpp -o derpy but I have a no such file or directory error about QMainWindow (I installed the full Qt5 group in Arch).
I also tried qmake, make then ./DesktopDerp but it does nothing.
Edit : Aww... maybe I'm missing the GIFs ? :P
Title: Re: [QT] Desktop Derp
Post by: Vogtinator on June 09, 2013, 12:57:55 pm
Quote
Edit : Aww... maybe I'm missing the GIFs ?
I downloaded a gif and it appears, I suggest using QMovie.isValid() ;)
BTW: Compiling with qt4 works, too.

Quote
EDIT: I'm having trouble with a systemtrayicon and a rightclick-menu, if anybody wants to help me :D
I may be able to help. Did you read http://qt-project.org/doc/qt-4.8/qsystemtrayicon.html#setContextMenu]http://qt-project.org/doc/qt-4.8/qsystemtrayicon.html#setContextMenu]http://qt-project.org/doc/qt-4.8/qsystemtrayicon.html#setContextMenu (http://qt-project.org/doc/qt-4.8/qsystemtrayicon.html#setContextMenu)?
It accepts a QMenu, like in http://qt-project.org/doc/qt-4.8/mainwindows-menus.html (http://qt-project.org/doc/qt-4.8/mainwindows-menus.html)

BTW2: http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/ (http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/) :P
Title: Re: [QT] Desktop Derp
Post by: Streetwalrus on June 09, 2013, 01:45:59 pm
Yeah in README.md it says Qt5 but in DesktopPonies.pro it says "greaterThan(QT_MAJOR_VERSION, 4)".

I've put a GIF and hardcoded the name to see it work, it looks like Derpy's just randomly moving around. You need to make her follow the mouse like Juju's ! :D
Edit : Whoops, didn't read the OP correctly I guess. :P Seriously, you owe it to the world. :trollface:
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 09, 2013, 02:33:16 pm
Yeah, i still have to upload the gifs, and i compile it over qt creator, it says it uses these lines (untested):
qmake /home/sorunome/desktopponies/DesktopPonies/DesktopPonies.pro -r -spec linux-g++-64 CONFIG+=debug CONFIG+=declarative_debug CONFIG+=qml_debug
make in /home/sorunome/desktopponies/build-DesktopPonies-Desktop-Debug

it just has to create the c++ files out of the .ui files and then make in the new directory.

EDIT: Yeah Vogtinator, i'll take a look, thanks

And I noticed a bug O.O after like a sertain number of gifs it just can't load new ones anymore, any idea why?
Title: Re: [QT] Desktop Derp
Post by: Vogtinator on June 09, 2013, 02:36:23 pm
Quote
And I noticed a bug  after like a sertain number of gifs it just can't load new ones anymore, any idea why?
While reading the source I noticed you're not freeing "QDesktopWidget *desk = new QDesktopWidget;", maybe that's the issue?
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 09, 2013, 02:40:03 pm
maybe, how do i free that? I've never done something like that in C++ >.>
Title: Re: [QT] Desktop Derp
Post by: Vogtinator on June 09, 2013, 02:40:58 pm
As simple as it sounds:
Code: [Select]
desk = new QDesktopWidget;
free desk;

Edit: If you don't use pointers, it'll be done automatically if it gets out of scope, like this:
Code: [Select]
{
QDesktopWidget desk;
}
//desk will be freed
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 09, 2013, 02:47:06 pm
As simple as it sounds:
Code: [Select]
desk = new QDesktopWidget;
free desk;


uho, i need to decalre by QDesktopWidget *desk = new QDesktopWidget;
and free desk gives me the error that it expects a ; before desk

EDIT: but for soem reason i don't think it is the desk, because i just use that to check the boundries of the screen every frame.
Title: Re: [QT] Desktop Derp
Post by: Vogtinator on June 09, 2013, 02:49:18 pm
Oh, sorry, I mistook free for delete,
Code: [Select]
delete desk; should work.
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 09, 2013, 02:50:49 pm
Ok, let's see, it'll take like half an hour or longer.

BTW, can I somehow overgo the minimizing on when somkebody hits the show desktop button?
If not, can I somehow maximize my window?
Title: Re: [QT] Desktop Derp
Post by: Streetwalrus on June 09, 2013, 02:50:56 pm
Hmmm... That's why one shouldn't use two languages that are similar in the core but with little differences. :P
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 09, 2013, 03:05:27 pm
I added now the gifs to the repo :D
Title: Re: [QT] Desktop Derp
Post by: Streetwalrus on June 09, 2013, 03:06:18 pm
Ah cool. Pulling. :D
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 09, 2013, 03:08:07 pm
just make sure to move the gifs to your build directory, otherwise it won't work. It will compile+run without the gifs but there just won't be a derpy then.
Title: Re: [QT] Desktop Derp
Post by: Streetwalrus on June 09, 2013, 03:14:27 pm
Ok it works. BTW to compile you just do qmake then make.
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 09, 2013, 03:18:12 pm
really, that is awesome, i just looked at what qt creator sais it does, all that i need to do to compile is hit ctrl+r :P
And glad that you could compile it :D

EDIT: so Vogtinator, your fix doesn't work, it still stops running the new gifs after some time/some amount of gifs played, idk which one it is.
Title: Re: [QT] Desktop Derp
Post by: Vogtinator on June 09, 2013, 03:55:58 pm
Quote
It will compile+run without the gifs but there just won't be a derpy then.
You can embed the .gifs as resource (e.g. /gifs/<name>.gif) and load them with ":/gifs/<name>.gif" as filename.

Quote
Ok it works. BTW to compile you just do qmake then make.
Better practice is to mkdir build, cd build, qmake .. and then make, so the source directory will be kept clean.
It's easier to exclude build in .gitignore than Makefile, *.o and the other output files.

Quote
, i just looked at what qt creator sais it does, all that i need to do to compile is hit ctrl+r
ctrl+b = build and F5 = debug.
BTW: If you use qDebug() << "asdf" instead of std::cout << "asdf", debug output will only be printed in debug build.

Quote
so Vogtinator, your fix doesn't work, it still stops running the new gifs after some time/some amount of gifs played, idk which one it is.
F5 will help you ;)

Edit: Some more advice:
- Don't allocate a QMovie everytime you change the animation. Set the filename by using QMovie::setFileName instead.
- To get the screen size use
Code: [Select]
QRect screen = qApp()->desktop()->screenGeometry()- You can use the << operator instead of .append
Code: [Select]
possibleMoveActions.append("fly");
possibleMoveActions.append("flyupsidedown");
//Is the same as
possibleMoveActions << "fly" << "flyupsidedown";
-Use initialization lists:
Code: [Select]
//The contructor
derp() {
posx=500;
posy=500;
}
//does (almost) the same as
derp() : posx(500), posy(500)
{}
-You don't have to use an .ui file, you can add a QLabel as property of your MainWindow and add it using setCentralWidget(label)
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 09, 2013, 11:34:20 pm
thanks for all the tips! so what is the difference with
Code: [Select]
//The contructor
derp() {
posx=500;
posy=500;
}
//does (almost) the same as
derp() : posx(500), posy(500)
{}
as almost is not all the way :P

and that was how i append the lists, i didn't know how to use qlists :P

and a somehow wanne use a ui file, but this project is actually too simple to use one so i should step away from it, just lemme code a bit, maybe magically the bug with derpy vanishing will disappear :D
Title: Re: [QT] Desktop Derp
Post by: Vogtinator on June 10, 2013, 08:52:35 am
thanks for all the tips! so what is the difference with
Code: [Select]
//The contructor
derp() {
posx=500;
posy=500;
}
//does (almost) the same as
derp() : posx(500), posy(500)
{}
as almost is not all the way :P
With initialization lists you can call the constructor of your parent classes and initialize const members.
http://www.cprogramming.com/tutorial/initialization-lists-c++.html (http://www.cprogramming.com/tutorial/initialization-lists-c++.html)
Actually, what happens in
Code: [Select]
class Foo {
private: int i;
public: Foo()
{
i = 1;
}
};
Is that i will be initialized to 0 first (default constructor) and later its value will be set.
Quote
and a somehow wanne use a ui file, but this project is actually too simple to use one so i should step away from it, just lemme code a bit, maybe magically the bug with derpy vanishing will disappear :D
That bug hasn't occured to me at all, but I noticed it's still invisible without .gifs.
Title: Re: [QT] Desktop Derp
Post by: Sorunome on June 13, 2013, 03:46:01 am
I guess it is somehow filling up it's ram, i think that I fixed it, i'm testing it atm, i'll just run her for houres ^^

EDIT: All that I got around changing so far is
Quote
- Don't allocate a QMovie everytime you change the animation. Set the filename by using QMovie::setFileName instead.
and the bug seem to be fixed, i'll still do the other suggestions though :D