For the following, I get self.page1.sizeHint() and self.page1.minimumSizeHint() both as QSize(-1, -1). Does anyone know why? I was expecting the minimum size hint should be at least the size of the label.
# ...
self.page1 = QtGui.QWidget()
self.page1.setObjectName("page_General")
self.label_Server = QtGui.QLabel(self.page1)
self.label_Server.setGeometry(QtCore.QRect(20, 20, 39, 13))
self.label_Server.setObjectName("label_Server")
print self.page1.sizeHint(), self.page1.minimumSizeHint()
Thanks
The default implementation of sizeHint():
This property holds the recommended size for the widget.
If the value of this property is an invalid size, no size is
recommended.
The default implementation of sizeHint() returns an invalid size if
there is no layout for this widget, and returns the layout's preferred
size otherwise.
Same goes for minimumSizeHint.
Related
I want to have a computed property that tracks that historical max of another property. Both these attributes are a member of an ndb class.
My naive approach was the following:
number = ndb.IntegerProperty(default=0)
highest_ever_number = ndb.ComputedProperty(lambda self: self.highest_ever_number if self.highest_ever_number >= self.number else self.number)
The intent was to set the highest_ever_number to a new number if number ever surpassed highest_ever_number.
This doesn't work because highest_ever_number is initially unset. I cannot set it to a default value using "default=0". Is there a workaround to this?
Thanks
I don't think you can use such conditions inside a Computed Property. This whole functionality can be done within a function, so it doesn't need to be a ComputedProperty. A better alternative to this problem could be to create a class method and call it whenever necessary.
Instead of using a computed property, you should use a normal IntegerProperty but use a _pre_put hook (https://cloud.google.com/appengine/docs/standard/python/ndb/modelclass#hooks) to verify/update highest_ever_number.
I can't test it right now, but this might do the trick:
number = ndb.IntegerProperty(default=0)
highest_ever_number = ndb.ComputedProperty(lambda self: self.highest_ever_number if
self.highest_ever_number and self.highest_ever_number >= self.number else self.number)
I am struggling to get the height that is required for a UILabel based on variable sized text that it could accept. From hours of research, I have not yet discovered a viable way of achieving this. Currently my code is as follows:
func getHeightForTitle(postTitle: NSString) -> CGFloat {
// Get the height of the font
let constraintSize = CGSizeMake(self.cellTextWidth, CGFloat.max)
let attributes = [NSFontAttributeName: [UIFont.systemFontOfSize(16.0)]]
let labelSize = postTitle.boundingRectWithSize(constraintSize,
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: attributes,
context: nil)
return labelSize.height
}
This however throws the following error:
2014-08-02 12:09:37.370 Testing App[8365:351906] - [_TtCSs23_ContiguousArrayStorage00007FD26C15F708 pointSize]: unrecognized selector sent to instance 0x11e640050
This is always thrown at the let labelSize = postTitle... method and I believe it is down to the attributes variable. I however maybe wrong. Any help is appreciated, and much thanked!
Please note: This is for an iOS 8, Swift development project.
Two observations. First, what's wrong with your code is that this line is not Swift:
let attributes = [NSFontAttributeName: [UIFont.systemFontOfSize(16.0)]]
The stuff after the equals sign is Objective-C, and Swift is having trouble interpreting it. Remove the square brackets from around the UIFont call; you have turned this into an array, which is the source of the error you're seeing.
Second, and more important, there are many much simpler ways to do this, by letting the label tell you its size for the desired text:
Put the text into the label and call sizeToFit() on the label.
Put the text into the label and call sizeThatFits() on the label with the desired width and large height.
Under auto layout, set the label's preferredMaxLayoutWidth to the desired width and put the text into the label; it will size itself.
However, I would urge you not to do this if you don't have to. A label is already self-sizing under auto layout, and in iOS 8 there's a new feature where a table cell will self-adjust its height to its contents, so there is now very rarely a need to pre-measure a label's dimensions.
Put the text into the label and call sizeThatFits() on the label with the desired width and large height.
sub_label=UILabel(frame: CGRectMake(0, 0, self.view.bounds.width, 50))
sub_label.numberOfLines=0;
sub_label.textAlignment=NSTextAlignment.Left
sub_label.lineBreakMode=NSLineBreakMode.ByWordWrapping
let subfont = UIFont(name: "Helvetica", size: 20.0)
sub_label.font=subfont
sub_label.text="his is just a load of texthis is just a load of texthis is just a load of texthis is just a load of texthis is just a load of texthis is just a load of text"
sub_label.backgroundColor=UIColor.clearColor()
var textViewSizesub=sub_label.sizeThatFits(CGSizeMake(self.view.bounds.width, CGFloat.max))
sub_label.frame=CGRectMake(0, textViewSize.height, self.view.bounds.width-5, textViewSizesub.height)
Does anyone know of a work around or what I am currently doing wrong here.
CGSize boundingSize = CGSizeMake(288, 9999);
CGRect boundingRect = [text boundingRectWithSize:boundingSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:[NSDictionary dictionaryWithObjectsAndKeys:theFont, NSFontAttributeName, nil]
context:nil];
When the code runs on iOS7 (both on the phone and sim) the size returned is 416.3 wide with a height of 15.5. Clearly the boundingRectWithSize is just being ignored. Has anyone else come across this and if so has anyone an idea on how to fix it or can someone point me to where I am going wrong.
As per the Apple documentation:
You can use this method to compute the space required to draw the
string. The constraints you specify in the size parameter are a guide
for the renderer for how to size the string. However, the actual
bounding rectangle returned by this method can be larger than the
constraints if additional space is needed to render the entire string.
Typically, the renderer preserves the width constraint and adjusts the
height constraint as needed.
If you specify a fixed font and too small a space, something has to give. In this case, it's the bounding space. I'm guessing you expected line wrapping. Does a width of 288 allow any reasonable wraps?
I knows that this is old question, but I found a workaround for your problem. below is the code example
1) First create a macro for minimum height
#define MIN_HEIGHT 10.0f
2) After that use below code to give variable height based on your text specified. But for this you need to specify the frame for UILabel OR whatever you are using for displaying the text.
// Initialize UILabel with initial frame.
UILabel *lblMakeModel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 180, 50)];
// Set numberOfLines as zero
lblMakeModel.numberOfLines = 0;
// Set text here
lblMakeModel.text = #"sdbsbdjhsbdhjsbdhjasd bhbdhjasbdsahjdbahjsdbjhsd bdhjsabdhjsbdhsbdhsad dhbsadbasdhbsajhdbsadyogi";
// create a constraint for fixed width and maximum 20000 height.
CGSize constraint = CGSizeMake(lblMakeModel.frame.size.width, 20000.0f);
// Get the CGRect with the given constraint for the text of UILabel
CGRect textRect = [lblMakeModel.text boundingRectWithSize:constraint
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:#{NSFontAttributeName:lblMakeModel.font}
context:nil];
// Set LineBreakMode for UIlabel
[lblMakeModel setLineBreakMode:NSLineBreakByWordWrapping];
[lblMakeModel setAdjustsFontSizeToFitWidth:NO];
// Again set the frame from the height you get from CGRect object.
[lblMakeModel setFrame:CGRectMake(lblMakeModel.frame.origin.x, lblMakeModel.frame.origin.y, lblMakeModel.frame.size.width, MAX(textRect.size.height, MIN_HEIGHT))];
There is a real bug still exist in the method "boundingRectWithSize", sometimes it gives wrong width greater than the limited width, i solved it by this work around and get the label height after the method "sizeToFit"
UILabel *textLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0,maxTextWidth, CGFLOAT_MAX)];
[textLabel setNumberOfLines:0];
[textLabel setFont:Font];
[textLabel setLineBreakMode:NSLineBreakByWordWrapping];
[textLabel setText:#"Text bla bla bla"];
[textLabel sizeToFit];
float height = textLabel.frame.size.height;
Is it at all possible to calculate the width size (in inches) of a particular string in the default font ( par()$family ) , and default size ( par()$ps ).
Calculating the height seems relatively simple:
par()$ps * 1/72
The challenge with width is that it is dependent on the string itself. A string of i's "iiiiii" is smaller than a string of o's "ooooooo"
You are looking for the function strwidth in base
strwidth('this is cool', font = 12, units = 'in')
While setting a QGraphicsItem rotation, I get different results upon the transformation origin point while using setRotation() and using:
transform = QTransform()
transform.rotate(myAngle)
myItem.setTransform(transform)
In both portion of code, I set setTransformOriginPoint() to the same point.
Results are:
While using setRotation() method, the item is rotated upon its transformation origin point.
While using the QTransform object, the item is rotated upon item's origin, that is, point (0,0).
My code is more complex than that, but I think It applies the same. The QGraphicsItem is in fact a QGraphicsItemGroup and I can check the issue adding just one item, and in my rotation procedure change the setRotation() method for the QTransform object. The latter, ignores the setTransformOriginPoint().
I'm having this issue for a while, and I dig a lot of resources. I browse the Qt C++ code, and I can see that the setRotation() method modifies a field calles rotation (a real value) in the TransformData structure within the QGraphicsItem. The origin point is also a two field real value in such a structure called xOrigin and yOrigin respectively. The transformation is stored in the tranform field. All this information is used in a variable called: transformData.
So, I don't get why the transformation set in the transformData->transform field is ignoring the values transformData->xOrigin and transformData->yOrigin at the time of being applied.
The code I used to test that issue is the following relevant part (I have an rotate item that receives mouse inputs and applies rotation to the item itself):
# This method using QTransform object....
def mouseMoveEvent(self, event):
if self.pressed:
parent = self.parentItem()
parentPos = parent.boundingRect().center()
newPoint = event.scenePos()
iNumber = (newPoint.x()-parentPos.x())-((newPoint.y()-parentPos.y()))*1j
angle = cmath.phase(iNumber)+1.5*math.pi
self.appliedRotation = (360-math.degrees(angle))%360 - self.angleOffset
transform = QTransform()
transform.rotate(self.appliedRotation)
self.parentItem().setTransform(transform)
# ...Against this one using setRotation()
def mouseMoveEvent(self, event):
if self.pressed:
parent = self.parentItem()
parentPos = parent.boundingRect().center()
newPoint = event.scenePos()
iNumber = (newPoint.x()-parentPos.x())-((newPoint.y()-parentPos.y()))*1j
angle = cmath.phase(iNumber)+1.5*math.pi
self.appliedRotation = (360-math.degrees(angle))%360 - self.angleOffset
self.parentItem().setRotation(self.appliedRotation)
On both, previously the setTransformOriginPoint() is set, but it's not a relevant part to show the code, but just to know that it is done.
I'm getting frustrated to not find a solution to it. As it seems so straightforward, why setting a rotation transformation matrix does not use the transformation origin point that I have set and while using setRotation() method works fine? That question took me to the source code, but now is more confusing as rotation is keeping separated from the transformation applied...
I was solving the same problem. I found out that QGraphicsItem::setTransformOriginPoint() is accepted only for QGraphicsItem::setRotation(). It is ignored for QGraphicsItem::setTransform().
I use this code to reach the same behavior for QTransform():
transform = QtGui.QTransform()
centerX = item.boundingRect().width()/2
centerY = item.boundingRect().height()/2
transform.translate( centerX , centerY )
transform.rotate( -rotation )
transform.translate( -centerX , -centerY )
item.setTransform( transform )