As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I want to create a very simple game, something like this. In general I need several colors of squares, they should appear, disappear, move while you click on them. I have created a QDialog. In it I use MyDialog::mousePressEvent(QMouseEvent *event) function in order to get the coordinates where mouse was clicked. In general I need two more things to programm my game.
Put images in my QDialog
Move them
Remove them form my dialog
Could you give some sample code that adds an image into the dialog, moves it and destroys, please? What is the most convenient way of doing this?
Place and image in specific coordinates in QGraphicsScene
Functions is the code are useful for creating a simple game: item.setPos, scene.addItem ...
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to make a timeline in Qt, where a different color stands for a different task.
Ultimately, it should look a bit like this (but only one line);
Now anyone got any ideas how to do this without installing extra libraries, but just with QPaint?
The data it is representing is stored in a self-defined structure.
Any help is welcome.
Thx!
You seem to need something like the code below, although please note that it is just pseudo code, and the exact details depend a lot on how you get the data from your datastructure which you have not yet shared with us.
...
QPainter *painter = new QPainter(MyPaintDevice); // MyPaintDevice could be even 'this'
QPen pen(Qt::gray, 2);
painter->setPen(pen);
int currentX = 0;
const int currentY = 0;
const int height = hardCodedValue; // Coming from some static data initialization
foreach (const Settings& settings, settingsList) {
QBrush brush(QColor(settings.colorString()));
painter->setBrush(brush);
painter->drawRect(currentX, currentY, settings.width(), height);
currentX += settings.width();
}
...
Admittedly, you would be better off going for QML rather than the old QPainter engine for several reasons. That is hardware accelerated these days rather than software rasterization as the QPainter approach, but probably more importantly for you: it is lotta simpler.
You would need to look into the native Rect qml element, and probably the Repeater.
You can subclass Qwidget, and redefine at least paintEvent(QPaintEvent * event) and resizeEvent(QResizeEvent * event).
I assume your widget hold a initial list of pairs of QRect and QColor. When a resize event occurs you compute a rendering list of QRect with basic relative arithmetic. When a paint even occurs you use this rendering list to fill with the color.
You can even allow to modify colors with mouse events for some neat rendering.
All of these can be done with Qml too, and may be even easier.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Frameworks like Qt and GTK+ offer to developers some standard user controls like editboxes, comboboxes, etc. But many GUI-applications have more complicated user controls. For example, text editors have areas where user can input text and insert images or tables. Audio editors usually display waveforms of audiotracks, volume indicators, etc. My question is how such controls are drawn? Moreover, these controls usually can handle events - for example, tables in text editors can be selected, on right mouse button click some context menu pops up, etc. So, problem is not only to draw these controls, but also to make controls handle events. I have only idea to use Canvas or OpenGL, but I suspect that it would be difficult to implement events handling. May be, there are more simple ways to draw user interface?
This is a pretty vague question, so this will be a somewhat vague answer. In general, most of this work has already been done. For example, Qt already has a text view class, an image class, and a table class (I'm not very experienced with Qt, just going from general stuff I know). For the text editor you are talking about, you might derive a class from a Qt text editing widget which has a handler to accept the dragging of images and can draw them. Again, Qt already has the code to draw images and do all kinds of painting.
To draw an audio waveform, you would have some sort of box or widget (generally speaking) that would draw a vertical line for each sampling interval to represent the volume or amplitude there.
Yes, OpenGL would be overkill for what you are talking about here because you would need to somehow write your own event handling, whereas most GUI frameworks do that for you. Again, a rather vague answer for a vague question, but I hope it helps!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have seen a couple topics about this already but they were a bit vague for me so I decided to make this. I'm working on a little adventure game just for fun in Qt, its basically just text on the screen and the player inputs commands into a line edit widget which I then process and do the related action/event. Thing is, I am a little confused on how to approach this. I don't want to dig myself a hole by manually coding in lots of commands and events per room because it seems to just be a pain later on. So then I thought about using some sort of database to store the information but which one should I use? I would love some advice from people who have tried something similar.
And here, these pictures are a rough outline of what I am trying to do.
Flowchart of states
Level tiles
Edit: I should add, the tiles for the level basically work like this. The light gray is a direction the player can move in, the dark gray parts are walls and the colors are various different actions you can do.
I don't particularly care for code, but I would like suggestions on what tools to use for this and maybe how to set them up right. Someone must know.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I have a complex drawing on a Gtk DrawingArea widget and I wish to provide the user with a way to select a rectangle on it to expand for a closer view. I have managed to get the necessary mouse button events sorted out so that the rectangle can be selected, but it would be desirable to have the actual rectangle drawn on the display, moving around along with the mouse. I need to know how one does this without disturbing the underlying drawing (i.e. so I can erase the temporary rectangle as it changes size and shape without having to redraw the underlying picture).
Could someone tell me the correct name for this effect (so that I can google it) or does anyone know of an example, or a keyword to search the Gtk documentation?
Thank you.
You are looking for the Rubber Band technique.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
as the title says, I want to add my main window a banner in the bottom of the window that shows text that runs from left to right, just like you can see in your TV when watching fox/CNN...
I'm using QT 4.5.2
Thanks a lot :)
You have many options. You could:
Create a custom widget and override QWidget::paintEvent()
Drawing your text to an image and then repainting that image as often as needed
Creating a QGraphicsScene and then moving an appropriate text item around.
QTimeline and/or QTimer will likely be useful in your implementation. The key idea is that you need to draw something at different positions at consistent intervals. If you redraw frequently enough, it will look like an animation.