How to obtain the geometry of a subwidget in QMainWindow - qt

I want to get the actual geometry of a promoted widget in the QMainWindow. Here below is a screenshot of the main window design.
The renderImage is a promoted widget using a defined renderImage class, which is used for image painting. Now I want to get the actual position of the widget in the QMainWindow in renderimage.cpp, in order for painting at that region where the widget is located.
Is there some method for get this geometry information. I know in the mainwindow.cpp, such geometry information can be accessed:
QRect rect = ui->renderImage->geometry()
But then how to inform renderImage.cpp of the result? Thanks!

Related

Displaying a Pixmap on a Graphics View Widget Object in Qt

I am trying to display a pixmap on the Graphic View widget. I believe what it is missing is a way to actually link the QGraphicsScene "scene" to the widget I have on the form, seeing that you can create multiple Graphics View Objects there must be a way to explicitly state which Graphics View item you want the pixmap to be displayed on. The Graphics View widget object is called PixMapView, using the same name as my GraphicsView object in my code did not change the functionality of my program. The code below compiles but just shows a blank white Graphics View object. My goal is to create a diagonal line on the pixmap and display it on the Graphics View Object. Previously the first section of code, all the way to the start of the while loop, worked with fillRect and some Auto-fill backround code. I believe the problem is purely in the section of code after the while loop *The while loop is essentially the only part not from Qt documentation. So in short could someone look at this code and see why my pixmap is not displaying on my Graphics View Widget.
CanvasTest::CanvasTest(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CanvasTest)
{
ui->setupUi(this);
QPixmap pm(200,200);
QPainter pmp(&pm);
pmp.setPen(Qt::black);
int counter = 0;
while (counter < 200)
{
pmp.drawPoint(counter,counter);
counter++;
}
pmp.end();
QGraphicsScene scene;
QGraphicsPixmapItem item(pm);
scene.addItem(&item);
QGraphicsView view(&scene);
view.show();
}
What the form looks like with the Graphic View Widget
I would like to display the diagonal line on the widget presented in the image
ui->(whatever name your widget has)->( any method your widget is associated with)
Note: this is due to the automatic code ui->setupUI(this);

Parenting an object in PyQt causes the object to be owned by Qt instead of PyQt. What are the consequences?

In the QObject.__init__(self, Parent=None) documentation it states:
The parent argument, if not None, causes self to be owned by Qt instead of PyQt.
What does it mean to be owned by Qt instead of PyQt? Does this have effects on behavior that I should be aware of when developing a PyQt application?
Yes, It is. It have Effects to before create any object in PyQt application. If we set parent QWidget None, I can be tell this QWidget is top-level window. I will try explain;
"Parent Widget equivalent like frame of windows"
Parent Widget of QMainWindow is none. Then, owned by Qt instead of PyQt (or easy word root). Parent Widget of QWidget (in red) is QMainWindow, Then geometry reference by QMainWindow. Parent Widget of QPushButton is QWidget (Green), Then geometry reference by QWidget (Green). And all widget as same ...
So, QMainWindow have fully control widget in your child. For example, If self delete, All child will follow delete too. etc.
But, If we set Parent Widget of QWidget (in red) is None, that mean this QWidget like new window. Like this picture;
For now, we have 2 top-level window. But QWidget have independent with QMainWindow. If self delete, All child will follow delete too but not outside QMainWindow. Outside QWidget not delete. So, some application have create many widget, but it will has main widget in main menu to control child.
Not only frame of windows, It just Example & Comparison.
Regards,

How to get the size of QGraphicsView that is in a layout?

I'm new to Qt programming and I am developing a drawing application. I have a class MyWidget which has a member QGraphicsView. MyWidget is a member of another class MainWidget (which has other widgets as well and all of them are in a layout).MainWidget is the central widget of my subclass of QMainWindow.
I have created functions to scale the view with the mouse wheel event and a function to drag the scene around.
The problem is - I want to set the Scene's size to be fixed, and to be 3 times the size of the view, but since the view is managed by a layout in order to take as much space as possible I can't get the view's size?
Any help appreciated.
the size property will give you the current size of your widget:
http://qt-project.org/doc/qt-4.8/qwidget.html#size-prop
Alternatively, you could subclass QGraphicsView and re-implement the resizeEvent:
http://qt-project.org/doc/qt-4.8/qwidget.html#resizeEvent
For a full example, have a look into:
http://qt-project.org/doc/qt-4.8/widgets-scribble.html
You can definitely get the view's size. There are two ways to go about it:
Attach an event filter object to your view: myView->installEventFilter(filterObject). The filterObject's eventFilter method will be invoked for all events reaching your view, including resize events.
Use a custom view class and reimplement resizeEvent. This method gets called each time the widget is resized. Do note that designer allows you to add custom classes without having to write plugins.

Qt : execute a slot each time a QTableWidget change its geometry?

I've made a widget derived from QTableWidget and I would like to execute a slot each time the a column or a line is resize, and each time the widget is resized. How to do that as there is no signal for that ?
Thank you very much.
For resizing of the widget itself: Inherit from QTableView, add a signal reimplement resizeEvent. In your resizeEvent emmit your new signal and call the base implementation of resizeEvent to actually do the resizing.
For resizing of columns: Use the signals from the associated QHeaderView which you can obtain with horizontalHeader

How to show pixel position and color from a QGraphicsPixmapItem

I'm developing a custom widget with QGraphicsScene/View and I have no prior experience with it.
The custom widget is an image viewer that needs to track mouse movement and send signal(s) to it's parent dialog / window. The signal(s) will be the position of the pixel under the mouse cursor and it's color (in RGB). A status bar will use that information.
I use a QGraphicsPixmapItem to display the image I load from a file in the scene.
Thanks.
First of all you have to implement the mouseMoveEvent in your custom item. In this function you can easily get the mouse position calling the pos function. You can get the rgb value if you transform the item's pixmap into image and call the pixel function. You should consider storing the QImage as member variable in order to avoid multiple transformations. Finally you have to emit a custom signal. Sample code follows:
void MyPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
{
QPointF mousePosition = event->pos();
QRgb rgbValue = pixmap().toImage().pixel(mousePosition.x(), mousePostion.y());
emit currentPositionRgbChanged(mousePosition, rgbValue);
}
Notice that QGraphicsItems do not inherit from QObject so by default signals/slots are not supported. You should inherit from QObject as well. This is what QGraphicsObject does. Last but not least I would advise you to enable mouse tracking on your QGraphicsView
I found the mouseMoveEvent approach to not work at all, at least not with Qt5.5. However, enabling hover events with setAcceptHoverEvents(true) on the item and reimplementing hoverMoveEvent(QGraphicsSceneHoverEvent * event) worked like a charm.
The Qt docs on mouseMoveEvent() provide the clue:
"If you do receive this event, you can be certain that this item also received a mouse press event"
http://doc.qt.io/qt-5.5/qgraphicsitem.html#mouseMoveEvent

Resources