Results 1 to 5 of 5

Thread: Expansion rules for a blank widget

  1. #1
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Expansion rules for a blank widget

    At the moment I've got a program setup with a main QHBoxLayout containing two widgets.
    The right widget is a QTableView, and the left widget is a "blank" widget what I'm using to draw shapes is (basically a paint window). The only dimensional data I've given the "paint" widget is a recommended height and width using the sizeHint() function.
    When I expand the whole MainWindow, the paint widget's width remains fixed, while the table widget expands to fill the new area.

    A couple of questions:

    1. What is the easiest way to make both widgets' widths increase evenly as the main window expands?
    2. Is there a way I can add some sort of "drag bar" in between the widgets to enable resizing of both widgets while leaving the overall window size unchanged (i.e. increase one widget's width as the other decreases?
    3. Is it easy to add scroll bars to blank widgets? I want to be able to paint objects on a large area with dynamic size (i.e. often increasing or decreasing in size), but I'm not sure how I would go about doing this Are there any useful examples someone can point me to? Would it simply be a matter of adding scroll bars and setting the maximum width/height according to the furthest point I currently have painted to?

  2. #2
    Join Date
    May 2010
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Expansion rules for a blank widget

    I suggest you take a look a QGraphicsView as a replacement for your "Paint" widget. It should have comparable sizing to your table, and you can put a QGraphicsScene of arbitrary size in it. It automatically supports dragging the scene around, and if you subclass the View, you can add an event listener for the mouse wheel to drive zooming (you'll like the QGraphicsView::scale() function for that). As for the "drag bar" check out QSplitter. I have only used it from the GUI designer, not from code, so you'll have to check the docs on that one for usage.

  3. #3
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Expansion rules for a blank widget

    Wow... QSplitter was exactly what I was after for controlling the size of the child widgets.
    I'm about to try out QGraphicsView, and I suspect it will be exactly what I'm after!
    Thanks for the help.

  4. #4
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Expansion rules for a blank widget

    Quote Originally Posted by grabalon View Post
    ... if you subclass the View, you can add an event listener for the mouse wheel to drive zooming (you'll like the QGraphicsView::scale() function for that).
    Would the below be a logical way to setup my QGraphicsView andQGraphicsScene objects for my program?

    I have created a new class, "graphicsEditorWidget" for displaying in my main window as the "CentralWidget", and in the constructor I've basically created a new QGraphicsScene, a QGraphicsView, and an ellipse item to test that it's displaying correctly (which it appears to be). Then I've created a layout for the class to display. Is there anything you would recommend me doing differently?

    Qt Code:
    1. graphicsEditorWidget::graphicsEditorWidget(graphicsData *data, QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QGraphicsScene *graphScene = new QGraphicsScene(this);
    5. graphScene->setSceneRect( -100.0, -100.0, 200.0, 200.0 );
    6.  
    7. QGraphicsEllipseItem *item = new QGraphicsEllipseItem(0,graphScene);
    8. item->setRect( -50.0, -50.0, 1000.0, 1000.0 );
    9. item->setFlag(QGraphicsItem::ItemIsMovable);
    10.  
    11. QGraphicsView *graphView = new QGraphicsView(graphScene);
    12. graphView->setRenderHints( QPainter::Antialiasing );
    13.  
    14. QVBoxLayout *graphLayout = new QVBoxLayout;
    15. graphLayout ->addWidget(graphView);
    16. setLayout(graphLayout);
    17. }
    To copy to clipboard, switch view to plain text mode 

    How exactly do I go about "subclassing the View" (C++ noob question, no doubt)? Should I be making graphicsEditorWidget a subclass of "QGraphicsView" instead of "QWidget"?

  5. #5
    Join Date
    May 2010
    Posts
    30
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Expansion rules for a blank widget

    Nice work so far. The graphicsEditorWidget is good to keep for ui interface purposes. You can use it to connect mouse and keyboard events to appropriate slots within both your View and Scene. To sub-class you'll need an entirely new class (I'll call it MyGraphicsView for lack of a better name). Declare it as follows:
    Qt Code:
    1. class MyGraphicsView : public QGraphicsView
    2. {
    3. Q_OBJECT
    4. public:
    5. MyGraphicsView(QGraphicsScene*, QWidget* parent = 0);
    6. protected slots: //Use this if you want the zoom features
    7. void wheelEvent(QWheelEvent* event);
    8. }
    To copy to clipboard, switch view to plain text mode 
    The constructor is fairly straightforward:
    Qt Code:
    1. MyGraphicsView::MyGraphicsView(QGraphicsScene* s, QWidget* parent) : QGraphicsView(s, parent) { }
    To copy to clipboard, switch view to plain text mode 
    I'll let you fill in other class features as you see fit, but I suggest the wheel event function looks something like:
    Qt Code:
    1. void MyGraphicsView::wheelEvent(QWheelEvent *event)
    2. {
    3. if(event->delta() > 0)
    4. scale(1.1111,1.1111);
    5. else
    6. scale(0.9,0.9);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Finally, to actually use the View, change line 11 in your code above to:
    Qt Code:
    1. MyGraphicsView* graphView = new MyGraphicsView(graphScene);
    To copy to clipboard, switch view to plain text mode 
    See if that get's you going on how this all works.

Similar Threads

  1. Suppress QSettings Expansion of @ Symbol
    By ChrisW67 in forum Newbie
    Replies: 0
    Last Post: 19th April 2010, 05:03
  2. Mastering signals; what's your opinion on Delta Object Rules?
    By piotr.dobrogost in forum Qt Programming
    Replies: 1
    Last Post: 14th October 2009, 14:05
  3. Drawing the expansion dialog widget
    By kaushal_gaurav in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2008, 10:54
  4. Build rules: Please assist
    By locus in forum General Programming
    Replies: 1
    Last Post: 4th April 2007, 08:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.