Qt-ROS添加Qt Designer Form Class实现窗口跳转
前提
实现效果
下列两个窗口之间可以随意跳转。
添加Designer class类
step1:新建项目,选择设计师类文件
step2:选择类型为widget
step3:将其添加到Qt-ROS项目里
将Qt文件树文件进行重命名
由于上一步自动添加的类后缀名是.h的,但是根据CMakeList.txt文件的内容,一般是.hpp和.cpp;所以重命名新建的Form Class文件的文件名,并添加到相应的文件夹后再打开。
在UI里添加控件
在ROS的UI和form.ui里添加控件pushbutton默认的控件名称。
存在问题
直接编译Build时候报错,无法识别form.h文件等其他内容;无法使用Designer Class执行跳转操作。
解决方法
在form.cpp里修改form.h的文件名为…/include/test/form.hpp
讲form.cpp文件默认的头文件名为#include “form.h”,修改为#include “…/include/test/form.hpp”
在mainwindow.hpp里修改form.hpp的文件名
在mainwindow.hpp里添加#include "form.hpp"头文件
代码
在form.hpp里添加跳转函数的信号和按键的槽函数
// form.hpp
#ifndef FORM_H
#define FORM_H
#include <QWidget>
namespace Ui {
class Form;
}
class Form : public QWidget
{
Q_OBJECT
public:
explicit Form(QWidget *parent = nullptr);
~Form();
public slots:
void slot_pushbutton();//槽函数
signals:
void close_and_open();//信号
private:
Ui::Form *ui;
};
#endif // FORM_H
在form.cpp里实现函数
// form.cpp
#include "../include/test/form.hpp"
#include "ui_form.h"
Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(slot_pushbutton()));
}
Form::~Form()
{
delete ui;
}
void Form::slot_pushbutton()
{
emit close_and_open();
this->hide();
}
在mainwindow.hpp里添加按键槽函数和跳转信号的槽函数
// mainwindow.hpp
/**
* @file /include/test/main_window.hpp
*
* @brief Qt based gui for test.
*
* @date November 2010
**/
#ifndef test_MAIN_WINDOW_H
#define test_MAIN_WINDOW_H
/*****************************************************************************
** Includes
*****************************************************************************/
#include <QtWidgets/QMainWindow>
#include "ui_main_window.h"
#include "qnode.hpp"
#include "form.hpp"
/*****************************************************************************
** Namespace
*****************************************************************************/
namespace test {
/*****************************************************************************
** Interface [MainWindow]
*****************************************************************************/
/**
* @brief Qt central, all operations relating to the view part here.
*/
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(int argc, char** argv, QWidget *parent = 0);
~MainWindow();
void ReadSettings(); // Load up qt program settings at startup
void WriteSettings(); // Save qt program settings when closing
void closeEvent(QCloseEvent *event); // Overloaded function
void showNoMasterMessage();
public Q_SLOTS:
/******************************************
** Auto-connections (connectSlotsByName())
*******************************************/
void on_actionAbout_triggered();
void on_button_connect_clicked(bool check );
void on_checkbox_use_environment_stateChanged(int state);
void slot_pushbutton(bool);//槽函数
void slot_qt_demo_show();//显示界面的槽函数
/******************************************
** Manual connections
*******************************************/
void updateLoggingView(); // no idea why this can't connect automatically
private:
Ui::MainWindowDesign ui;
QNode qnode;
};
} // namespace test
#endif // test_MAIN_WINDOW_H
在mainwindow.cpp里实现
// mainwindow.cpp
/**
* @file /src/main_window.cpp
*
* @brief Implementation for the qt gui.
*
* @date February 2011
**/
/*****************************************************************************
** Includes
*****************************************************************************/
#include <QtWidgets>
#include <QMessageBox>
#include <iostream>
#include "../include/test/main_window.hpp"
/*****************************************************************************
** Namespaces
*****************************************************************************/
namespace test {
using namespace Qt;
/*****************************************************************************
** Implementation [MainWindow]
*****************************************************************************/
MainWindow::MainWindow(int argc, char** argv, QWidget *parent)
: QMainWindow(parent)
, qnode(argc,argv)
{
ui.setupUi(this); // Calling this incidentally connects all ui's triggers to on_...() callbacks in this class.
QObject::connect(ui.actionAbout_Qt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt())); // qApp is a global variable for the application
connect(ui.pushButton,SIGNAL(clicked(bool)),this,SLOT(slot_pushbutton(bool)));
ReadSettings();
setWindowIcon(QIcon(":/images/icon.png"));
ui.tab_manager->setCurrentIndex(0); // ensure the first tab is showing - qt-designer should have this already hardwired, but often loses it (settings?).
QObject::connect(&qnode, SIGNAL(rosShutdown()), this, SLOT(close()));
/*********************
** Logging
**********************/
ui.view_logging->setModel(qnode.loggingModel());
QObject::connect(&qnode, SIGNAL(loggingUpdated()), this, SLOT(updateLoggingView()));
/*********************
** Auto Start
**********************/
if ( ui.checkbox_remember_settings->isChecked() ) {
on_button_connect_clicked(true);
}
}
MainWindow::~MainWindow() {}
/*****************************************************************************
** Implementation [Slots]
*****************************************************************************/
void MainWindow::showNoMasterMessage() {
QMessageBox msgBox;
msgBox.setText("Couldn't find the ros master.");
msgBox.exec();
close();
}
/*
* These triggers whenever the button is clicked, regardless of whether it
* is already checked or not.
*/
void MainWindow::slot_pushbutton(bool)
{
Form *f = new Form;
f->show();
connect(f,SIGNAL(close_and_open()),this,SLOT(slot_qt_demo_show()));
this->hide();
}
void MainWindow::slot_qt_demo_show()
{
this->show();
}
void MainWindow::on_button_connect_clicked(bool check ) {
if ( ui.checkbox_use_environment->isChecked() ) {
if ( !qnode.init() ) {
showNoMasterMessage();
} else {
ui.button_connect->setEnabled(false);
}
} else {
if ( ! qnode.init(ui.line_edit_master->text().toStdString(),
ui.line_edit_host->text().toStdString()) ) {
showNoMasterMessage();
} else {
ui.button_connect->setEnabled(false);
ui.line_edit_master->setReadOnly(true);
ui.line_edit_host->setReadOnly(true);
ui.line_edit_topic->setReadOnly(true);
}
}
}
void MainWindow::on_checkbox_use_environment_stateChanged(int state) {
bool enabled;
if ( state == 0 ) {
enabled = true;
} else {
enabled = false;
}
ui.line_edit_master->setEnabled(enabled);
ui.line_edit_host->setEnabled(enabled);
//ui.line_edit_topic->setEnabled(enabled);
}
/*****************************************************************************
** Implemenation [Slots][manually connected]
*****************************************************************************/
/**
* This function is signalled by the underlying model. When the model changes,
* this will drop the cursor down to the last line in the QListview to ensure
* the user can always see the latest log message.
*/
void MainWindow::updateLoggingView() {
ui.view_logging->scrollToBottom();
}
/*****************************************************************************
** Implementation [Menu]
*****************************************************************************/
void MainWindow::on_actionAbout_triggered() {
QMessageBox::about(this, tr("About ..."),tr("<h2>PACKAGE_NAME Test Program 0.10</h2><p>Copyright Yujin Robot</p><p>This package needs an about description.</p>"));
}
/*****************************************************************************
** Implementation [Configuration]
*****************************************************************************/
void MainWindow::ReadSettings() {
QSettings settings("Qt-Ros Package", "test");
restoreGeometry(settings.value("geometry").toByteArray());
restoreState(settings.value("windowState").toByteArray());
QString master_url = settings.value("master_url",QString("http://192.168.1.2:11311/")).toString();
QString host_url = settings.value("host_url", QString("192.168.1.3")).toString();
//QString topic_name = settings.value("topic_name", QString("/chatter")).toString();
ui.line_edit_master->setText(master_url);
ui.line_edit_host->setText(host_url);
//ui.line_edit_topic->setText(topic_name);
bool remember = settings.value("remember_settings", false).toBool();
ui.checkbox_remember_settings->setChecked(remember);
bool checked = settings.value("use_environment_variables", false).toBool();
ui.checkbox_use_environment->setChecked(checked);
if ( checked ) {
ui.line_edit_master->setEnabled(false);
ui.line_edit_host->setEnabled(false);
//ui.line_edit_topic->setEnabled(false);
}
}
void MainWindow::WriteSettings() {
QSettings settings("Qt-Ros Package", "test");
settings.setValue("master_url",ui.line_edit_master->text());
settings.setValue("host_url",ui.line_edit_host->text());
//settings.setValue("topic_name",ui.line_edit_topic->text());
settings.setValue("use_environment_variables",QVariant(ui.checkbox_use_environment->isChecked()));
settings.setValue("geometry", saveGeometry());
settings.setValue("windowState", saveState());
settings.setValue("remember_settings",QVariant(ui.checkbox_remember_settings->isChecked()));
}
void MainWindow::closeEvent(QCloseEvent *event)
{
WriteSettings();
QMainWindow::closeEvent(event);
}
} // namespace test