I am using Qt creator 4.2.1. I have installed Qwt lib successfully and been able to drag and drop QwtPlot widget to my form. I have an custom class Plot:public QwtPlot. I wish I can add QwtPlot to my form and then perform a widget promote to Plot.
However, when I try to do so, the promote dialog box does not show QwtPlot as a base class that I can choose. If I select QWidget as the base class and then specify "Plot" and "plot.h" as my promote class and header file, I got build errors complaining redefine of "class Plot". I do have the plot.h in my project already and it seems uic creates another "class Plot" for me. Hence, the compiler complains my plot.h has an redefined class.
How to resolve this problem? Thanks!
Place a widget from toolbox and use promote and make sure your class structure is like following sample.
#ifndef PLOT_H
#define PLOT_H
#include <QObject>
#include <qwt_plot.h>
class Plot : public QwtPlot
{
Q_OBJECT
public:
explicit Plot(QWidget *parent = nullptr);
signals:
public slots:
};
#endif // PLOT_H
Related
I want to make a button to stay pushed after a click. So I made a slot make_pushed which I try to use for that purpose. The button which was clicked on is identified by QObject::sender() method. But something goes wrong since it doesn't work.
QPushButton * size=new QPushButton("size",this);
connect(size, SIGNAL(clicked()), this, SLOT(make_pushed()));
void Window4::make_pushed()
{
QObject* sender = this->sender();
QPushButton* button = qobject_cast<QPushButton*>(sender);
button->setDown(true);
button->setText("Yep");
}
class Window4 : public QWidget
{
public:
Window4(QWidget * parent=0);
private slots:
void make_pushed();
};
There's a mistake in application output "QObject::connect: No such slot QWidget::make_pushed() in" , although everything compiles and the window appears. The problem is the slot is apparently not found, although it is in the same cpp file and in the header. And therefore when clicked, the botton neigher changes its text nor stays pushed.
You just forgot Q_OBJECT macro in class declaration http://doc.qt.io/qt-5/qobject.html:
Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.
http://doc.qt.io/qt-5/qobject.html#Q_OBJECT:
The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.
Note: This macro requires the class to be a subclass of QObject. Use Q_GADGET instead of Q_OBJECT to enable the meta object system's support for enums in a class that is not a QObject subclass.
Just use it like this every time you subclass QObject/QWidget/...:
#include <QObject>
class Counter : public QObject
{
Q_OBJECT
// ...
}
To put it simply, I want a new class that extends a custom widget that I've made, and thus have full access to it's UI.
I've tried several different methods so far based on how one would normally subclass/extend classes, but I'm somehow failing horribly for so many different reasons.
Additionally, simply using my widget as a member in my new class wouldn't do for this situation.
Can someone illustrate a quick example of how I would do this? I have done a bunch of searching but I can't seem to find any hits relating to exactly what I'm trying to do
If all else fails I will simply copy over the code and make an actual new widget, which technically would have saved me lots time, but it just doesn't feel right doing that.
My first instinct was to do something like this ( Qwe being my new class, Asd being the widget ):
class Qwe : Asd {public: ...}
And I even made the widget's Ui public, but then I just got the error :
use of undefine type Ui::Asd
whenever I tried to access the Ui's elements.
Let's say we have a custom widget named BaseWidget and a child widget named ChildWidget. Declare BaseWidget as usually, but make its ui member protected instead of private, like this:
protected:
Ui::BaseWidget *ui;
Declare ChildWidget as an ordinary widget derived from BaseWidget. Make sure you include ui_BaseWidget.h in the ChildWidget.cpp file, just as you do it in BaseWidget.cpp (this include and the header itself is generated by Qt).
Header:
#include "BaseWidget.h"
class ChildWidget : public BaseWidget {
Q_OBJECT
public:
explicit ChildWidget(QString text, QWidget *parent = 0);
};
Source:
#include "ChildWidget.h"
#include "ui_BaseWidget.h"
ChildWidget::ChildWidget(QString text, QWidget *parent) :
BaseWidget(parent)
{
ui->label->setText(text);
}
I've created a qt widgets application. Using the design mode I've created a QTextEdit and indicated that in the header file:
...
QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
class QTextEdit;
QT_END_NAMESPACE
...
private:
Ui::MainWindow *ui;
QTextEdit *textEdit_2;
};
There is also a slot which is triggered by pushing a button. What it has to do is to insert some text into textEdit_2 after the button is pushed, still the program crashes.
In mainwindow.cpp:
void MainWindow::on_action_4_triggered()
{
textEdit_2->setText("text");
}
I've also tried
textEdit_2->setText(QString("text"));
which anyway doesn't work. What's the problem?
textEdit_2->setText("text");
The problem is that you are trying to ignore the actual text widget created in QtDesigner and invent another as a class member. This is not going to fly as you seem to want it.
In order to reuse the text widget from the UI that you created with the graphical tool, you would need to reuse the ui object as follows:
ui->textEdit_2->setText("text");
Please also note that you do not need to construct QString explicitly like this:
textEdit_2->setText(QString("text"));
This will be all automatic for you.
So I am currently writing a Qt app, but am fairly new to it and am unsure of how certain things should be designed. As I add more and more code, my MainWindow.cpp is getting large and unruly. I am curious of what is the proper way to separate my code up in to smaller files. Each of the components that I wish to move to a separate file is making changes to the UI. What I am currently doing is literally just creating a new .cpp file, and then including my MainWindow and also the MainWindow ui. Here is an example of a function that I placed in its own file.
#include <QDebug>
#include <QString>
#include <QPalette>
#include "master_main_window.h"
#include "ui_master_main_window.h"
#include "cmd_net.h"
#include "cmd.h"
/*
* Send a command/data packet to the host
*/
void MasterMainWindow::sendCommand() {
// Disable some GUI components
ui->con_btn_cmd_disc->setEnabled(false);
ui->con_btn_cmd_rec->setEnabled(false);
ui->cmd_edit->setEnabled(false);
ui->cmd_btn_send->setEnabled(false);
// Send the packet through the open socket
sendCmdPacket(ui->cmd_edit->text().toLocal8Bit().data(), cmdSocket);
// Enable the socket notifier
cmdSockNotifier->setEnabled(true);
qDebug() << "Command sent:"
<< (ui->cmd_edit->text().toLocal8Bit().data())
<< "\nSent Packet";
}
As you can see, I have simply included the "master_main_window.h" and "ui_master_main_window.h" which gives me access to all of the different functions/variables/ui available in the MainWindow class. I am curious if I am doing this the proper way, or if there is a better method to achieve my goal of separating the functions into separate files.
If I'm getting what you're asking correctly, since you're working with pointers here you can simply write other classes in different files and pass your variables in ui to them.
For example, let's say you're ui has these variables in it:
QWidget * leftWidget;
QWidget * centerWidget;
QWidget * rightWidget;
You can write classes who inherit QWidget and give these variables to them, like this:
class leftWidgetClass : public QWidget
{
//your code here
}
and so on...
And then in the constructor of your MainWindow you can do this:
leftWidget = new leftWidgetClass();
rightWidget = new rightWidgetClass();
centerWidget = new centerWidgetClass();
The proper way would be to not using ui namespace and mainwindow methods outside of mainwindow class. You should sublass QWidget or any other classes, which functionality you want to extend, create all the functionality you need (like doImportantStuff()) and include this new class header ( let's call it myWidget) in mainwindow.h. Then you can create those myWidget's in mainwindow, add them to ui (ether through disigner, add QWidget, click promote to , select your class, or by adding to layout manually) and use all of their signals and functionality, that you've created, like:
connect(ui->doWorkButtot(),SIGNAL(clicked()), myWidget, SLOT(doImportantStuff()));`
again, changes to ui you can do via signals and slots mechanism; In myWidget when you feel, you have to change ui somehow, emit a signal and catch it in mainwindow with connect. Example:
myWidget.h:
...
signals:
void needUiChange();
....
public slots:
myWidget::doImportantStuff(){
....
emit needUiChange();
// you can emit signals with params also:
// emit setToolTipText(QString("from signal));
...
and in mainwindow catch signal with connect:
connect(myWidget, SIGNAL(needUiChange(),this,SLOT(updateUI());
// actually you can connect directly to let's say `pushButton`'s slot `hide()`
I'm following the Gtk "Hello World" tutorial found here, and I've come across a line in a class declaration I've never seen before (I've only been learning to program for a few months now), and I was wondering if someone could please explain it to me. The line is the
class HelloWorld : public Gtk::Window
I know what class HelloWorld is doing, but I've never seen the public Gtk::Window before. The full header file is provided for reference.
#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H
#include <gtkmm/button.h>
#include <gtkmm/window.h>
class HelloWorld : public Gtk::Window
{
public:
HelloWorld();
virtual ~HelloWorld();
protected:
//Signal handlers:
void on_button_clicked();
//Member widgets:
Gtk::Button m_button;
};
#endif // GTKMM_EXAMPLE_HELLOWORLD_H
It means that HelloWorld is derived from Gtk::Window, so it inherits its behaviour.
HelloWorld represents a Gtk window, so it is just natural to have it derive from the Gtk's window class. It's constructor will probably add a button to the window (the actual window is created by the parent class constructor, which is invoked automatically when a new instance of HelloWorld is created …) and connect a signal handler (on_button_clicked) to the window.
You can call all of Gtk::Window's methods through an instance of HelloWorld. In turn, HelloWorld can override virtual methods of Gtk::Window to change its behaviour.
class HelloWorld : public Gtk::Window
This means the class HelloWorld is publically derived from class Window defined inside Gtk namespace. Gtk::Window is the fully qualified name of that class.
I just want to point out that you should either be using the 3.0 branch of gtkmm or you should use the stable branch of the tutorial.link text
The 3.0 branch of gtkmm is still under development, and you should expect a few "surprises" now and then.