Change polygon size on window resize - qt

In my QML application I am drawing a triangle manually using "Canvas" object. The problem is I cannot figure out how to change the drawn object size each time I resize the main window.
Preferably, it would be convenient If I could simply redraw the triangle each time a window resize occurs. But I don't know how could this be done in QML. In bare QT, I guess I would subscribe to window size changed signal. What is the proper way of doing this in QML?
Edit: The program is described here: Change polygon color dynamically
In my main window there is a rectangle called rectMain. It is always the same size as the window. Then inside that rectangle there is another one, called rectTemp. In that rectangle I draw the canvas.
Edit 2: So far I have figure out how to react manually on window size changes:
property int lastWindowWidth: 0
property int lastWindowHeight: 0
function windowSizeChanged()
{
if ((lastWindowWidth == width) && (lastWindowHeight == height))
return;
console.log("New height: ", height, " New width: ", width);
lastWindowWidth = width
lastWindowHeight = height
}
onHeightChanged: windowSizeChanged();
onWidthChanged: windowSizeChanged();

Related

Inside a QWidget, Qwt plot is there then if i am trying to resize widget simultaneously plot should resize. how to apply resizeevent to QWidget

I have applied resizeEvent to a QWidget. I am trying to resize the widget inside another widget. I have a QWT plot that should also resize.
I have applied resizeEvent to the QWidget and resized to the plot:
void SampleWidget:: resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
int diffofWidth = event->size().width() - event->oldSize().width();
int diffofHeight = event->size().height() - event->oldSize().height();
QSize size = plot->size();
if ((event->oldSize().width() >= 0) && (event->oldSize().height() >= 0))
{
if (event->size().width() <= this->minimumWidth() && event->size().height() <= this->minimumHeight())
{
return;
}
else
{
plot->resize(size.width() + diffofWidth, size.height() + diffofHeight);
plot->replot();
}
}
}
The widget is able to resize, but after resizing many times, the plot is only displayed partially in the widget. I am not able to see whole content in the widget. What's going on here?
Assuming you are asking why your widget isn't resizing properly, you likely have minimum sizes set within the layouts. Be sure that your minimum size of the parent is greater than or equal to the sum of it's children, that could cause the child elements to be partially hidden.
Also, check sizehint and the minimum size set for each object, increase them as needed.

TitleWindow position on screenreen in Flex 3

I would like to position the window on the screen as TitleWindow image attached to an applicat![enter image description here][1]ion because I'm using is necessary that he is to be called exactly that position. How could he do?
http://www.techall.com.br/Doubt/TitleWindow.jpg
After creating a TitleWindow you can set its position just by setting the x/y values.
var popup_CADASTRO = new CADASTRO;
PopUpManager.addPopUp(popup_CADASTRO ,this,true);
popup_CADASTRO.x = 20; // x position where you want to put the window
popup_CADASTRO.y = 40; // y position where you want to put the window
You don't need to do the PopUpManager.centerPopUp.

PyQt/Qt: How to stretch an image in Qlabel widget?

I want to display an image in my app. I use QtDesigner to design UI, then use pyqt to coding. The problem is the image that will be shown is lager than the widget size on the UI. I refer to offical demo:
QT - Widget Image Viewer Demo
add imagelabel and scrollArea, code as follows:
---- UI init ----
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(40, 140, 361, 511))
self.label.setSizePolicy(QtGui.QSizePolicy.Preferred,QtGui.QSizePolicy.Preferred)
self.label.setObjectName(_fromUtf8("label"))
self.scrollArea = QtGui.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(40, 140, 361, 511))
self.scrollArea.setWidget(self.label)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
---- function ----
filename = "./Penguins.jpg"
image = QtGui.QImage(filename)
pp = QtGui.QPixmap.fromImage(image)
lbl = QtGui.QLabel(self.label)
lbl.setPixmap(pp)
self.scrollArea.setWidgetResizable(True)
lbl.show()
but it doesn't stretch the image, even no scroll bar appear!
You need to call self.label.setScaledContents(true);. So that QLabel will resize itself to the size of pixmap/image and scroll-bar will get visible. See this documentation.
The default implementation of QLabel::setScaledContents wasn't working for me, since it didn't allow me to keep the aspect ratio when the images where larger then the label's
maximum sizes.
This little helper will scale the image down to fit into a label's maximum size if needed (but not up), always keeping the aspect ratio:
/**
* Fill a QLabel widget with an image file, respecting the widget's maximum sizes,
* while scaling the image down if needed (but not up), and keeping the aspect ratio
* Returns false if image loading failed
****************************************************************************/
static bool SetLabelImage(QLabel *label, QString imageFileName)
{
QPixmap pixmap(imageFileName);
if (pixmap.isNull()) return false;
int w = std::min(pixmap.width(), label->maximumWidth());
int h = std::min(pixmap.height(), label->maximumHeight());
pixmap = pixmap.scaled(QSize(w, h), Qt::KeepAspectRatio, Qt::SmoothTransformation);
label->setPixmap(pixmap);
return true;
}
I do not use PyQt but the QtPixmap control has scaled() functions. You can resize the image before put in the label:
scaled()
scaledToHeight()
scaledToWidth()
This is the sample code I use in C++ to resize an image to the QLabel size:
imatge.load("sprite.png");
QPixmap imatge2 = imatge.scaled(ui->label->width(),ui->label->height());

Flex scrolling more than 10.000 pixels

The Flex Canvas container is limited to 10,000x10,000 pixels. Yet, I've seen Flex apps that are scrolling way more than 10,000 pixels. Any idea how this can be done?
The content I want to scroll is in pieces already, but I need to add those pieces to something that can scroll vertically more than 10,000 pixels.
Depending on what you actually want to display you may be able to split your content into tiles. This is how Google Maps works, every time the map is moved the program determines which tiles are visible on the screen and loads them in. Any markers or overlays that are on the map are notified that the map has moved and determine where their new location is. If their location is off the screen they can be removed from the canvas. For example, the width of all the tiles at zoom level 20 on Google Maps is (256 pixels * 2^20) which equals 268,435,456 total pixels.
Essentially you just need to create a special Sprite that keeps track of the actual x,y location it is supposed to be positioned at. Any time the container moves you simply iterate through all of the child objects and determine where to put them.
function onCanvasScroll() {
//determine the top left coordinates of the canvas
//you will figure out how to do this depending on how the scrolling window
//is implemented
var canvas_scroll_x;
var canvas_scroll_y;
//create a bounding box for the canvas
var view_bounds = new Rectangle(canvas_scroll_x, canvas_scroll_y, canvas.width, canvas.height);
for (child in canvas) {
var x = child.actual_x - view_bounds.x;
var y = child.actual_y - view_bounds.y;
var childBounds = new Rectangle(x, y, child.width, child.height);
//determine if the component is visible on screen
if (view_bounds.intersects(child_bounds)) {
child.visible = true;
child.x = x;
child.y = y;
}
else {
child.visible = false;
}
}
}
So if you have a canvas that is positioned at (100, 20000), a sprite that is positioned at (300, 20100), and a window that is (640,408), you would place it at (200, 100) and it would be visible on the screen.
Instead of just setting visible to true or false a better approach would be to remove the items from the canvas entirely when they are not within the bounds of the view.

Terminal emulation in Flex

I need to do some emulation of some old DOS or mainframe terminals in Flex. Something like the image below for example.
The different coloured text is easy enough, but the ability to do different background colours, such as the yellow background is beyond the capabilities of the standard Flash text.
I may also need to be able to enter text at certain places and scroll text up the "terminal". Any idea how I'd attack this? Or better still, any existing code/components for this sort of thing?
Use TextField.getCharBoundaries to get a rectangle of the first and last characters in the areas where you want a background. From these rectangles you can construct a rectangle that spans the whole area. Use this to draw the background in a Shape placed behind the text field, or in the parent of the text field.
Update you asked for an example, here is how to get a rectangle from a range of characters:
var firstCharBounds : Rectangle = textField.getCharBoundaries(firstCharIndex);
var lastCharBounds : Rectangle = textField.getCharBoundaries(lastCharIndex);
var rangeBounds : Rectangle = new Rectangle();
rangeBounds.topLeft = firstCharBounds.topLeft;
rangeBounds.bottomRight = lastCharBounds.bottomRight;
If you want to find a rectangle for a whole line you can do this instead:
var charBounds : Rectangle = textField.getCharBoundaries(textField.getLineOffset(lineNumber));
var lineBounds : Rectangle = new Rectangle(0, charBounds.y, textField.width, firstCharBounds.height);
When you have the bounds of the text range you want to paint a background for, you can do this in the updateDisplayList method of the parent of the text field (assuming the text field is positioned at [0, 0] and has white text, and that textRangesWithYellowBackground is an array of rectangles that represent the text ranges that should have yellow backgrounds):
graphics.clear();
// this draws the black background
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, textField.width, textField.height);
graphics.endFill();
// this draws yellow text backgrounds
for each ( var r : Rectangle in textRangesWithYellowBackground )
graphics.beginFill(0xFFFF00);
graphics.drawRect(r.x, r.y, r.width, r.height);
graphics.endFill();
}
The font is fixed width and height, so making a background bitmap dynamically isn't difficult, and is probably the quickest and easiest solution. In fact, if you size it correctly there will only be one stretched pixel per character.
Color the pixel (or pixels) according to the background of the character.
-Adam

Resources