An image at a specific point in Qt - qt

I am trying to place a .png image(firm logo) at a specific point(coordinate). I've put several buttons, one after another, and now I want the image to be displayed just below these buttons. The code below should do the trick, but the coordinates are simply not working.
//QLabel myLabel; QVBoxLayout *layout; // class members, initialized with 'this'
QPixmap pixmap("v.png");
myLabel.setPixmap(pixmap);
myLabel.setMask(pixmap.mask());
myLabel.setGeometry(QRect(312, 454, 21, 20));
layout->addWidget(&myLabel);
How should I modify the code to simply include this image, possibly at a specific coordinate (just as with QPushButton)? Note that commenting out the last line removes the image, but, even when the image is shown, the buttons cannot be clicked (and they are not part of 'layout') Please provide code that would achieve image positioning with given coordinates.

QLayout is a controller which position widgets added to it. That is why it has no any sense to set coordinates to a widget and then place it to a layout. The layout will change the widget coordinates as soon as the widget is shown.
Futhermore, if you ask how to set certain position in coordinates, you shouldn't use QLayout at all, because it will change any position you set.
QPixmap pixmap("v.png");
myLabel.setPixmap(pixmap);
myLabel.setMask(pixmap.mask());
myLabel.setFixedSize(21, 20);
myLabel.move(312, 452); //ensure that this coordinates are in you widget
myLabel.show();

You will have to either show the label or put it in a layout or make sure it was given a parent in the constructor.
myLabel.setParent(parent_window)
The setParent method might also need you to show.
myLabel.show()
or
layout.addWidget(myLabel)

Related

How can I create multiple custom widgets and display them with their absolute position

So I currently have got a custom widget, and I want to add them to the main window after clicking a button. I would like to add them all to one fixed position first and then I will be able to drag them wherever I like. I am able to create and display these custom widgets with help of QHBoxLayout or QVBoxLayout, but in this case they will not be in the same position after I create them. Any help will be appreciated!
As the names suggest, the QLayout classes manage the position and geometry of the items added to them. You cannot move (eg. drag) an item out of a layout w/out first removing it from the layout (QLayout::removeItem() and derivatives). For example when you drag a toolbar or dock widget out of a QMainWindow it goes through all sorts of machinations to remove it from the MW layout, change the widget's window flags, remember the old position in the layout, and so on. And the reverse to dock it again.
To do what you describe (drag widgets arbitrarily around a window) you would need to not use a QLayout and position the widgets manually by specifying a QWidget::setGeometry() for example. After initial position, and assuming the user has some way to grab the widget (title bar or drag handle you made, etc), you'll probably still need to manage their positions, for example if the main window is resized (if you care about keeping them contained). Essentially you'd have a bunch of separate widgets acting as individual windows and probably need some way to keep track of them.
I don't know what kind of widgets you're talking about, but one option may be a QMdiArea which lets the user drag windowed widgets around, tabify them, save/restore state, and so on.
For more flexibility you could also look into the Qt Graphics Framework. The graphics scene has a lot of features for user-movable items/widgets, keeping track of them, and so on. It is probably the most flexible method overall, and you can also use regular QWidgets inside a graphics scene.
A couple other Q/A about arbitrarily positioning widgets (I'm sure there are more to be found):
QPushButton alignment on top another widget
How to keep Push Buttons constant in relative to change of Label Size in PyQt4

QTextEdit: How to add fixed position text on top of an image

I am using QTextEdit to implement an editor. One of the requirements I have is to add fixed position text on top of an image.
For example:
I have an image of dimensions: 300x300. I need to add text beginning at the location (20, 20) of the image and ensure that the text does not flow beyond the width of the image.
Something like below:
I am thinking that if I can add a QGraphicView, I can add the image and position text appropriately. Is this possible? Is there a way to introduce a graphic element into a QTextedit? If not, what is the right approach?
Is there a way to introduce a graphic element into a QTextEdit? If not, what is the right approach?
You could look at this the other way and add the QTextEdit to a QGraphicsScene. The graphics scene provides a QGraphicsProxyWidget to add standard Qt widgets.
Therefore, you could create a QGraphicsScene and QGraphicsView. Add a QGraphicsPixmapItem for the image and add the QTextEdit item with a call to QGraphicsScene::addWidget, which returns a QGraphicsProxyWidget, allowing you to position, scale and resize the widget.
Alternatively, you could start with a QGraphicsItem, inherit from that and create your own object which encapsulates the image and proxy object of the QTextEdit.
There are other ways of tackling this too, but I'd probably go for the custom QGraphicsItem. It also depends on your specification, but you can add text items in a graphics scene, without the QTextEdit, though you'd probably have to implement the editing feature, if this is required.

Qt ScrollArea on widget messes up size and position of widget [Qt 5.1]

I'm new with Qt and I want to implement a scrollable widget which can grow dynamically, e.g. by adding buttons into it when another button is pressed. I try to implement it using the following layout and code:
scrollArea = new QScrollArea(ui->outerWidget);
scrollArea->setWidget(ui->innerWidget);
layout = new QVBoxLayout(ui->outerWidget);
ui->innerWidget->setLayout(layout);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// code for PushButton to add buttons in innerWidget
void MainWindow::on_pushButton_clicked()
{
QPushButton *button = new QPushButton("button"+QString::number( nameCounter ));
nameCounter ++;
ui->innerWidget->layout()->addWidget(button);
}
This implementation has two problems when I run the program, first the innerWidget appears out of place (I define its position in Qt Creator's Design mode) and second after many widgets are added in the layout the scroll bar is doesn't grow, but instead the widgets are become smaller to fit into the layout:
In another thread it was suggested to set widgetResizable to true:
scrollArea->setWidgetResizable(true);
The scroll bar seems to work now but the new problem is that the innerWidget becomes very small so it is barely visible:
So, how can I achieve a scrollable widget that will respect the size and position of the design?
have no environment to verify but good luck.
to place scrollArea the target position,
Manually call SetGeometry, or
Place a QScrollArea in ui form, (suggested if it's static), or
Place a widget in your target position, and set QScrollArea's parent to it, and add QScrollArea to its layout, and Set QScrollArea to be expanding.
manually create a QWidget innerWidget and assign it to scrollArea via QScrollArea::setWidget(*QWidget), try different size policy to innerWidget, such as "Preferred".
Also be aware of Qt's reference mentioned : void QScrollArea::setWidget ( QWidget * widget )
Sets the scroll area's widget.
....
Note that You must add the layout of widget before you call this function; if you add it later, the widget will not be visible - regardless of when you show() the scroll area. In this case, you can also not show() the widget later.
Above list solutions, below are reasons to the problems you mentioned:
scrollArea->setWidget(ui->innerWidget); Setting a widget to ScrollArea will change the parenting and layouting of ui->innerWidget, so the geometry values written in ui form (in Qt Creater) will no longer take effect, this is the reason of innerWidget out of place. ui->innerWidget is no longer a child of outerWidget, it's geometry will follow its new parent (but not scrollArea, there's some tricky layouting inside QScrollArea". To be clear, innerWidget is not helpful to locate scrollArea in such scenario.
In your first clip of code, widget 'scrollArea' is created with parent outerWidget, again no size policy or layout or geometry is specified, so scrollArea will by default be placed at the left top corner of the parent "outerWidget". To place scrollArea to your target geometry, you can " set geometry manually " or " assign innerWidget as scrollArea's parent and expand scrollArea". Obviously the latter method cannot assign ui->innerWiget to scrollArea->setWidget().
scrollArea->setWidgetResizable(true); makes the scrollArea "shrink" at left top corner of outerWidget. This is because, QScrollArea does not increase along with it's contents, it can scroll to display all of it contents so the required size of QScrollArea can be as small as possible. Once the 'Resizable' property is set to "true", QScrollArea decides to shrink to its minimum necessary size, thus the size to display its scroll bar and scroll buttons....

QGraphicsView background

Hi i'm trying to get a photoshop-like behaviour for my QGraphicsScene
The grid in the background should not resize with the call of scale. And I must be able to save the picture with QPixmap::grabWidget(view) but without the background grid. I can probably do it with removing the background layer just before saving the picture, but i'm not sure if its cleanest way to do it.
Any ideas ?
thx.
Question 1
The grid in the background should not resize with the call of scale.
Use the QGraphicsItem::ItemIgnoresTransformations flag.
The item ignores inherited transformations (i.e., its position is
still anchored to its parent, but the parent or view rotation, zoom or
shear transformations are ignored). This flag is useful for keeping
text label items horizontal and unscaled, so they will still be
readable if the view is transformed. When set, the item's view
geometry and scene geometry will be maintained separately.
In order to set this flag use the setFlag function when creating the grid item.
Question 2
I must be able to save the picture with QPixmap::grabWidget(view) but without the
background grid.
Call the hide function on the grid item before calling the grabWidget. After you have grabbed it you show it again by calling the show function.

How to Add custom shape button in fixed Location in QWidget

I am new to Qt. First look at the image.
http://i225.photobucket.com/albums/dd200/saravanan_comp2001/sample.png
i already created custom shape button (inherit the QWidget). now the problem is adding the buttons into another widget(Parent QWidget).
How to add this button in Fixed location and some times i need to add the buttons in overlap.
i can't use any Layout method. Please help me to fix the problem
Just create your custom shape button with the QWidget as parent and set the position using the move function.

Resources