Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 952839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:57:28+00:00 2026-05-15T23:57:28+00:00

I have QGraphicsTextItem objects on a QGraphicsScene . The user can scale the QGraphicsTextItem

  • 0

I have QGraphicsTextItem objects on a QGraphicsScene. The user can scale the QGraphicsTextItem objects by dragging the corners. (I am using a custom “transformation editor” to do this.) The user can also change the size of the QGraphicsTextItem by changing the font size from a property panel. What I would like to do is unify these so that when the user scales the object by dragging the corner with the mouse, behind the scenes it actually is calculating “What size font is necessary to make the resulting object fit the target size and keep the scale factor at 1.0?”

What I am doing now is letting the object scale as normal using QGraphicsItem::mouseMoveEvent and then triggering a FinalizeMapScale method in QGraphicsItem::mouseReleaseEvent once the mouse scale is complete. This method should then change the font to the appropriate size and set the scale back to 1.0.

I have a solution that appears to be working, but I’m not crazy about it. I’m relatively new to both Qt and C++, so would appreciate any comments or corrections.

  • Is there a better way to architect this whole thing?
  • Are there Qt methods that already do this?
  • Is my method on the right track but has some Qt or C++ errors?

Feel free to comment on my answer below on submit your own preferred solution. Thanks!

[EDIT] As requested in comment, here is the basics of the scaling code. We actually went a different direction with this, so this code (and the code below) is no longer being used. This code is in the mouseMoveEvent method, having previously set a “scaling_” flag to true in mousePressEvent if the mouse was clicked in the bottom-right “hot spot”. Note that this code is in a decorator QGraphicsItem that holds a pointer to the target it is scaling. This abstraction was necessary for our project, but is probably overkill for most uses.

void TransformDecorator::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  ...
  if (scaling_) {
    QGraphicsItem *target_item = target_->AsQGraphicsItem();
    target_item->setTransformOriginPoint(0.0, 0.0);
    QPointF origin_scene = mapToScene(target_item->transformOriginPoint());
    QPointF scale_position_scene = mapToScene(event->pos());
    qreal unscaled_width = target_item->boundingRect().width();
    qreal scale_x = (scale_position_scene.x() - origin_scene.x()) / unscaled_width;
    if (scale_x * unscaled_width < kMinimumSize) {
      scale_x = kMinimumSize / unscaled_width;
    }
    target_item->setScale(scale_x);
  } else {
    QGraphicsObject::mouseMoveEvent(event);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:57:28+00:00Added an answer on May 15, 2026 at 11:57 pm

    Please no holy wars about the loop-with-exit construct. We’re comfortable with it.

    void MapTextElement::FinalizeMapScale() {
    
      // scene_document_width is the width of the text document as it appears in
      // the scene after scaling. After we are finished with this method, we want
      // the document to be as close as possible to this width with a scale of 1.0.
      qreal scene_document_width = document()->size().width() * scale();
    
      QString text = toPlainText();
    
      // Once the difference between scene_document_width and the calculated width
      // is below this value, we accept the new font size.
      const qreal acceptable_delta = 1.0;
    
      // If the difference between scene_document_width and the calculated width is
      // more than this value, we guess at the new font size by calculating a new
      // scale factor. Once it is beneath this value, we creep up (or down) by tiny
      // increments. Without this, we would sometimes incur long "back and forth"
      // loops when using the scale factor.
      const qreal creep_delta = 8.0;
      const qreal creep_increment = 0.1;
    
      QScopedPointer<QTextDocument> test_document(document()->clone());
      QFont new_font = this->font();
      qreal delta = 0.0;
    
      // To prevent infinite loops, we store the font size values that we try.
      // Because of the unpredictable (at least to me) relationship between font
      // point size and rendering size, this was the only way I could get it to
      // work reliably.
      QList<qreal> attempted_font_sizes;
    
      while (true) {
    
        test_document->setDefaultFont(new_font);
        delta = scene_document_width - test_document->size().width();
    
        if (std::abs(delta) <= acceptable_delta ||
            attempted_font_sizes.contains(new_font.pointSizeF())) {
          break;
        }
    
        attempted_font_sizes.append(new_font.pointSizeF());
    
        qreal new_font_size = 0.0;
        if (std::abs(delta) <= creep_delta) {
          new_font_size = delta > 0.0 ? new_font.pointSizeF() + creep_increment
                                      : new_font.pointSizeF() - creep_increment;
        } else {
          new_font_size = new_font.pointSizeF()
                          * scene_document_width
                          / test_document->size().width();
        }
        new_font.setPointSizeF(new_font_size);
      }
    
      this->setFont(new_font);
      this->setScale(1.0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
I inherited my scene from QGraphicsScene. I add many items(QGraphicslineItem, QGraphicsItem, QGraphicsTextItem) on this
Have an app that can use tts to read text messages. It can also
have written this little class, which generates a UUID every time an object of
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I am using the Qt QGraphicsScene class, adding pre-defined items such as QGraphicsRectItem ,
i have a scene with a multiple (QGraphicsTextItem)s, and i need to have control
Have converted devise new session from erb to Haml but doens't work, this is
I have about 150 QGraphicsLineItems in a QGraphicsScene, and I want to rotate them,
This is probably something very obvious, but I have a new to Qt and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.