QT 5 add a custom widget inherited from QGraphicsView to designer - c++

I have a custom widget from modified elastic nodes example. It inherits from QGraphicsView. In the example it is set as a central widget in the main window, so nothing can be added. Here's the code for main from the example:
#include "graphwidget.h"
#include <QApplication>
#include <QTime>
#include "mainwindow.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
GraphWidget *widget = new GraphWidget;
QMainWindow window;
window.setCentralWidget(widget);
window.show();
return app.exec();
}
I want to add the widget to the ui form, so that i can add some buttons and input. How can i do that?

What you are doing now is making an application.
If you want to make a custom widget in Qt , you need to build it as Custom Designer Widget - a plugin (as Dll).
In Qt creator choose OtherProject=>Qt Custom Designer Widget and create a project. You need to build as Release version, not the debug one.
After building, if Qt hasn't already, you need to copy the .dll file into Qt/plugins/designer and Qt/Tools/QtCreator/bin/plugins - you will find .dll's there. You also need to build plugin with tool chain, as Qt creator/designer was i.e. VisualStudio 2013 64bit or Mingw - MUST be the SAME.
It's not easy, but i wish you luck.

An easier way is to add QGraphicsView in the ui form and promote it to GraphWidget class.

Related

How can I test a full Qt5 GUI using qtestlib?

Using qtestlib with my Qt5 C++ widgets application, how can I test my full GUI?
In the documentation for qtestlib, it is explained how I could test an indivdual QWidget by simulating keypresses etc, however this seems impossible to do for a full UI, because the individual widgets of my UI are hidden inside the automatically generated ui_XXX.h file.
So how would I go about doing this?
In your ui files you can give names to the widgets. You can then search through the children from your toplevel widget.
For example to test that a Label reflects the characters entered into a LineEdit you could use:
MainWindow win;
QLineEdit *edit = win.findChild<QLineEdit *>("myLineEdit");
QTest::sendKeys(edit, "Example");
QLabel *label = win.findChild<QLabel *>("myLabel");
QCOMPARE(label->text(), "Example");

Creating, handling and switching between windows QTCreator

My research into the question suggests that I'm somewhat beating a dead horse, however I can't seem to get a conclusive answer.
I'm using QT Creator to create a GUI that will help interface with a register of units(simple objects, with some ID's and such).
I have a Main Menu, which contains 5 push buttons and a table. Pictured here. My project currently includes these files, and my main currently looks like this:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
UnitRegister e;
CommInterface c;
w.setRegistryPtr(e);
w.setCommPtr(c);
w.setTablePtr(w.findChild<QTableWidget*>("unitTable"));
w.show();
return a.exec();
}
What I want to do is press one of the four push-buttons on the left, and use that to switch to a different view in the main window.
For this purpose I considered using a QStackedWidget, and then having a page for each menu button. My question is this:
How do I/Should I create a separate QT Item Form Class(header, .cpp
and .ui file) for each page
How do I switch between the pages I've added using the QT Designer.
How do I ensure that the different pages can access the UnitRegister created in my main file.
I'm not necessarily searching for a complete answer, but something to get me going.
Switching Pages: Consider a QButtonGroup to give your buttons ids that you can map to the indexes of your QStackedWidget. Then you can do this:
connect(buttonGroup, SIGNAL(buttonClicked(int)), stackedWidget, SLOT(setCurrentIndex(int)));
Organizing the Pages: Create a .ui file and corresponding container widget for each page in your widget stack. This is much easier than one massive .ui file.
Accessing the UnitRegister: There are tons of ways to do this. Adding a setter function to your classes is one way.

Run a method from a C++ program with Qt without writing entire program in Qt [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is it possible to run a certain method from a C++ program with Qt with certain input parameters when for instance a button is clicked without writing the complete program in Qt itself?
Let's say that I have a project in Visual Studio with a corresponding .exe and I want to run the method example with input parameter 5 whenever a button is clicked. How could I do that?
Qt operates by creating a QWidget window, and then an event loop.
Your code to create a window, and then a button that triggers another function is trivial.
Many of the examples Qt provides show this functionality.
To get started, Download Qt. Download the Qt Add-On for Visual Studio. Note, that the express edition of visual studio doesn't work with the Qt Add-On.
After you have all that done, create a Qt project from the Qt classes that are available. In this case, I would not choose QML/Qt Quick, but just a Qt Widgets class, such as QWidget or QDialog or QMainWindow.
Using the Qt Add-On should ensure that you have the libraries found and available to Visual Studio.
Make sure that you can get a Hello World example is buildable and running using a Qt Widget of some sort.
The code it generates for you should look like this:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To add in a button, you need to place it on a Widget and then connect a SLOT to its clicked() signal. The following is a compact way to do it.
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QDebug>
#include <QApplication>
static void someFunction ()
{
qDebug() << "Button was clicked";
// run my other function that requires a parameter of 5
qDebug() << "Run my other function with 5!";
// otherFunction(5);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QPushButton *button = new QPushButton("Click Me");
QObject::connect(button, &QPushButton::clicked, someFunction);
QVBoxLayout *vbox = new QVBoxLayout();
vbox->addWidget(button);
w.setLayout(vbox);
w.show();
return a.exec();
}
To make a powerful GUI, you will want to leverage more than just a functor, and use QObjects and let it use the moc compiler and create your own signals and slots for all your custom functions.
Connecting to a functor is new with Qt 5.
http://qt-project.org/wiki/New_Signal_Slot_Syntax
http://woboq.com/blog/new-signals-slots-syntax-in-qt5.html
A more verbose way to do it, but allows for more expandability and OOP, is to subclass QWidget or QMainWindow, like it generated for you, and put a method in your subclassed function that you put under your slots list in your header file.
http://qt-project.org/doc/qt-5/signalsandslots.html
Take the time to understand signals and slots and you can create almost any GUI you can imagine using Qt. Good luck.
Hope that helps.

Qt: Access Widget from main function and implement exit button

I want to implement an exit button in my application, which has the following setup:
I have a main function which looks like this:
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
I also have a mainWindow function which has been generated by the QT Creator IDE.
I design the GUI with the Qt Designer and when I want a pushbutton to do something when clicked, I use a function like this:
void on_selection_clicked();
I hope the setup is now sufficiently described.
Now to my problem: I want to implement a button, which, when clicked, terminates the window and the application. I first tried implementing this in a function like this:
void on_exit_clicked();
But I don't know that to to here.
Then I heard of a aproach via QObject::connect, but I have two questions:
1.) Where should I put this? In the main function? 2.) can I access the object simply via the object name given in the QT Designer?
no you should connect it in the constructor of the MainWindow
connect(ui->exit,SIGNAL(clicked()),QCoreApplication::instance(), SLOT(exit()));
QCoreApplication::instance()->exit() will quit the application
yes through the ui field in MainWindow see the code above
I don't know which Qt version you use, so I will suppose Qt 5.0 (signal/slot mechanims was updated).
QWidget has slot QWidget::close().
QPushButton provides signal QPushButton::clicked(bool checked = false)
So you can connect them in constructor of your MainWindow:
QObject::connect(your_button, &QPushButton::clicked, this, &QWidget::close());
Also I suggest to look into the files generated from *.ui files - so you have deeper understanding of what's going on.

How to change the Title of the window in Qt?

How to change the title of the window in Qt? (Both for QDialog and QMainWindow.)
void QWidget::setWindowTitle ( const QString & )
EDIT: If you are using QtDesigner, on the property tab, there is an editable property called windowTitle which can be found under the QWidget section. The property tab can usually be found on the lower right part of the designer window.
For new Qt users this is a little more confusing than it seems if you are using QT Designer and .ui files.
Initially I tried to use ui->setWindowTitle, but that doesn't exist. ui is not a QDialog or a QMainWindow.
The owner of the ui is the QDialog or QMainWindow, the .ui just describes how to lay it out. In that case, you would use:
this->setWindowTitle("New Title");
I hope this helps someone else.
I know this is years later but I ran into the same problem. The solution I found was to change the window title in main.cpp. I guess once the w.show(); is called the window title can no longer be changed. In my case I just wanted the title to reflect the current directory and it works.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowTitle(QDir::currentPath());
w.show();
return a.exec();
}
You can also modify the windowTitle attribute in Qt Designer.
system("title WhateverYouWantToNameIt");