My project consists of operations between geometric figures on a cartesian plane.I would include a graph that have to be updated after each operation.
Thats the source:
http://pastebin.com/s5Fu9dHJ
I've created the wrapper "disegna" (: public QWidget) because of I am sending to display everything as separate widgets (I have a widget for the virtual keyboard, another for Qlineedits, etc.) and I need an QWidget object can be used with view-> addWidget (QWidget,int,int) because I cannot pass directly a QMainWindow object.
The Program run with no errors,but no "hello world" is drawed (and no blank-space for istance QGraphicView is created).
where am I doing it wrong?
Change
QGraphicsView view(&scene);
view.show();
to
QGraphicsView * view = new QGraphicsView(&scene);
view->show();
The way you have it now, the instance of QGraphicView is allocated on the stack and gets destroyed right after the disegna constructor is executed, that's why you can't see it.
Don't forget to free the memory.
Related
I'm trying to write a circuit designer software in QT on Linux. I'm using KDE 5 Plasma desktop and QTCreator as an IDE.
I tried to use QFrame paintEvent to paint on it, and it worked, but when im grabbed the window inside QFrame it moved.
I know about QGraphicsView, but i cant make a custom class and promote it based on that(it's not listed).
How can i create a custom class from a container(QFrame, QGraphicsView or anything) where i can override paint event and also it doesn't move window if i grab it?
Sorry for my poor english.
QGraphicsView inherits from QAbstractScrollArea which inherits from QFrame itself.
So you can keep the QFrame in the form, and keep it promoted to your canvas class, but simply make you canvas class inherit QGraphicsView instead.
Although, my Qt has two differences in behavior from the OP (but I don't use KDE):
Clicking on a QFrame and moving the mouse doesn't move the whole window for me. I guess this behavior for the OP could be changed by reimplementing void mousePressEvent ( QMouseEvent * event ) in the canvas class and giving it an empty code instead. (doc)
I can put QGraphicsView in my ui files, and I can right click on them to promote them to another custom-defined class.
Edit: Found the reason why the window moves on KDE!
i'm stuck with a simple Function of Qt that does not work for me.i made a class that inherits
from QMainWindow and another class that inherits from QWidget.then i made from the second a member object(a pointer to) inside the first and assigned it as its centralWidget during the construction of my window.
when it comes to adjust my centraWidget inside the window with the function QWidget::setGeomerty() it simply don't work.here's my code:
void MainWindow::show()
{
//some code that centers my window on the screen
int margin=this->width()/7;
centralWidget()->setGeometry(margin,centralWidget()->geometry().top(),this->width()-margin,centralWidget()->geometry().bottom());
QMainWindow::show();
}
i know it might be stupid but i just can't figure it out.help me.
QMainWindow has its own layout in which the center area is occupied by the central widget. So it won't be pretty straightforward to break that layout and modify the central widget size / position arbitrarily.
What I recommend is to use a placeholder central widget and add your widget as a child.
I'm pretty sure you can achieve what you want by setting a proper Qt built in layout to the "placeholder" central widget and then adding your widget to the layout.
layouts was necessary to manage what i want; i think it's not possible to hand directly over the central widget and try to move/resize it;but by adding a layout and a child widget, we can dispose of them.here is my code:
mainwindow.cpp
// i focused on my window constructor
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_MainView(new MainView(this)),
m_WindowLayout(new MainWindLayout(NULL))//my custom layout witch just inherits from QGridLayout
{
//.............
setCentralWidget(m_MainView);
m_WindowLayout=new MainWindLayout(m_MainView);//my layout set into my central Widget"m_MainView".
QWidget *temp(dynamic_cast<QWidget *>(m_MainView->centralView()));//centralView() returns a child QWidget of my central Widget
QMargins margins(this->width()/5,0,this->width()/5,0);//setting up layout margins :left,top,right,bottom;exactly what i need
m_WindowLayout->setContentsMargins( margins);
m_WindowLayout->addWidget(temp,0,0,-1,-1);//adding my child widget to the layout filling all cells of the gridlayout.
}
thanks everybody
I am working in Qt in my program. I have a widget window and I have placed a button and a list box in it. Then I have made a class called myplot which graph plotting is done. Then in my button event handler I have called the object of myplot
myplot * p1 = new myplot(session,session ,24, "session"); // send arrays in argument containing the data to be plot
p1->show();
myplot * p2 = new myplot(payload,payload ,24, "payload"); // send arrays in argument containing the data to be plot
p2->show();
It is working fine as my graph appear in new window, but what I want is that graphs should appear in my mainwidget window.
What I did next was to remove the title bar of my graphs window I wrote this
Qt Code:
p1->setWindowFlags(Qt::FramelessWindowHint);
p2->setWindowFlags(Qt::FramelessWindowHint);
Now
1) what should I do to place and append the graphs window in my main window? Also when I close main window my graph window should close.
2) when I select an other value from the list box and click button my old graph disappear and new should appear
i drag and droped a verticlalayout then write
ui->verticalLayout->addChildWidget(p1);
but got an error
/usr/include/qt4/QtGui/qlayout.h:191: error: ‘void QLayout::addChildWidget(QWidget*)’ is protected
then i draaged and droped a scrollarea and wrote
myplot * p1 = new myplot(session,session ,24, "session");
p1->setWindowFlags(Qt::FramelessWindowHint);
ui->scrollArea->addScrollBarWidget(p1,0);
p1->show();
my graphs stopped appearing kindly guide me am i doing it wrong or what the right way
Apparently, myplot is a childclass of QWidget. So you can just define a layout on your main window (e.g. using QHBoxLayout), and add those two widgets to it. This will cause them to be drawn inside the mainwindow. Also make sure that you pass the main window as the QWidget parent of the plots.
For replacing the plots, you can either remove the old plots from the layout and add new ones. But I would prefer to make the plots members of your main window and include some update method that would redraw with the new data
When you call show on widgets with no parent like that, it automatically gets wrapped in a window. What you should do is create one widget and add the graphs to it's layout. See QMainWindow examples, and section about dock widgets if that's what you want.
Basically you don't need to worry about the window they are appearing in, it is only appearing because they have no other parent.
What you need to do is to specify a parent widget when you construct them.
(This answer is based on the assumption that your plots are also QWidgets, if you could mention what class they actually are and perhaps link to the documentation it would really help us to answer you.)
In GTK, I used to have a window to which I gtk_container_add()'d a GtkVBox. Then I could pack widgets I wanted to the GtkVBox to have them appear in the window.
Now I've decided to try out Qt, but I can't seem to figure out how to do this in Qt.
Right now what I've done is create a QMainWindow, but I found that you can only pack one main widget into it, which is obviously quite limiting. So I wanted to create something like the GtkVBox and use that as the main widget and then add other widgets to this box.
What I've found by Googling are however only the Q3VBox widget, which seems to be what I want, but is deprecated, and the QVBoxLayout.
I tried to use the QVBoxLayout, but it tells me that I cannot change the layout of my QMainWindow since it already has a layout.
Edit: Here is how I do it (this is in the constructor):
box = new QVBoxLayout;
setLayout(box)
It compiles fine, but during runtime, it prints on the console:
QWidget::setLayout: Attempting to set QLayout "" on HCGWindow "", which already has a layout
(HCGWindow is my app's window, which is a subclass of QMainWindow)
So, how can I create something similar to a GtkVBox in Qt, and if the solution is the Q3VBox, why is it deprecated and what other thing should I use?
Thanks
In fact, here is the recommended solution provided by the Qt documentation.
You should create a QVBoxLayout and add the widgets you want in it. After that, you set the layout on another empty widget and then you set this widget as the central widget of the QMainWindow subclass. Here is an example in code:
QWidget* widget1 = new QWidget(); // This could be anything subclassing QWidget.
QWidget* widget2 = new QWidget();
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(widget1);
layout->addWidget(widget2);
QWidget* central = new QWidget(); // Only a containing QWidget.
central->setLayout(layout);
this->setCentralWidget(central);
Now, you QMainWindow subclass should have the two QWidgets in it, arranged in a QVBoxLayout.
Note here that I did not give any parent to anyone. You could have done it, but when you call addWidget or setCentralWidget, the ownership of the widget (and the layout) is given to the containing class.
If you read a bit about Qt, you'll know that this allows the parent to destroy his children when he is about to be destroyed itself.
Finally, note that QMainWindow is an exception and, from what I know, is the only class with setCentralWidget as a method. If you attempt to create a QWidget subclass, you will be able to use setLayout (as shown in the example above).
Hope this helps.
Try to create QVBoxLayout instance, add your widgets to it and add (not replace) this layout to main window's layout.
Note, QLayout class has no member addLayout, but subclasses has one.
Firstly you must get and remember classname of main window's layout:
qDebug(this.layout()->objectName);
Then add your QVBoxLayout to window's layout:
dynamic_cast<YourWindowLayoutClass>(
this.layout())->addLayout(your_qvboxlayout_object);
I hope it will work.
I am trying to get the QTabWidget automatically resized to fit the child tab when the child is added but have been unable to do so. I have created a form using Qt Designer and have inherited this using the single inheritance approach as follows.
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
private:
Ui::MyForm ui;
};
I have a QTabWidget and I am adding the instance of the object to my QTabWidget using the addTab(). When I display the QTabWidget, I notice that it hasn't resized to fit the MyWidget instance. What do I need to do to ensure that the QTabWidget instance gets automatically resized?
In designer, make sure you add a layout to your widget. Click on the widget's background so that way when you apply a layout, it applies to the whole widget. The trick is that the base (parent) widget that your form is built on needs a layout, and not just the items in the form.
Grid's are generally pretty easy to use. But sometimes the other ones are better. Designer can be tricky to use and takes a while to get used to. Basically every widget should probably have a layout applied to it. Strange things can happen when you don't.
I believe this is a bug of the Creator (at least for V4.7.4 opensource).
My solution is as following.
Do not create the QTabWidget object in the main project.
Create it independently (by creating Qt-> Form-class).
Then, copy the 3 files (.h, ,cpp, .ui) into the project, and add them to the project.
The result is: it resizes as the mainwindow resizing.
To fit any widget into its parent widget, you need to use a particular layout (grid, horizontal, vertical etc).