我在CMakeLists.txt中设置了这样一个项目:(这是一个以最大化大小打开的窗口。其中有一个按钮。当按钮被按下时,会出现一个弹出窗口)
cmake_minimum_required(VERSION 3.5)
project(ADB VERSION 0.1 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 COMPONENTS Quick Widgets REQUIRED)
set(SRC_DIR "${CMAKE_SOURCE_DIR}/src")
set(HEADERS_DIR "${CMAKE_SOURCE_DIR}/headers")
set(GLOB_RECURSE
"${SRC_DIR}/main.cpp"
"${HEADERS_DIR}/mainwindow.cpp"
"${HEADERS_DIR}/mainwindow.hpp"
"${HEADERS_DIR}/profilecreator.cpp"
"${HEADERS_DIR}/profilecreator.hpp"
)
include_directories(${SRC_DIR})
include_directories(${HEADERS_DIR})
qt_add_executable(
appADB
WIN32
"${GLOB_RECURSE}"
)
set_target_properties(
appADB PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_link_libraries(
appADB
PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
PRIVATE Qt${QT_VERSION_MAJOR}::Gui
)
install(TARGETS appADB
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(ADB)
endif()
我有MainWindow
类,只需按QPushButton
“createP”启动弹出窗口:
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QWidget>
#include <QDialog>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
~MainWindow();
private:
QDialog* popup;
};
#endif // MAINWINDOW_HPP
在主窗口中.cpp:
#include "mainwindow.hpp"
#include "profilecreator.hpp"
#include <QVBoxLayout>
#include <QGroupBox>
#include <QPushButton>
#include <QLineEdit>
MainWindow::MainWindow(QWidget* parent) : QWidget(parent)
{
QPushButton* createP = new QPushButton("Create profile");
createP->setStyleSheet("background-color: gray;");
createP->setFixedSize(100, 25);
createP->setToolTip("Create profile");
QHBoxLayout* creationControls = new QHBoxLayout;
controls->addWidget(createP);
QGroupBox* controlsBox = new QGroupBox;
controlsBox->setLayout(controls);
// Add box the main layout
QVBoxLayout* mainLayout = new QVBoxLayout;
// add controls
mainLayout->addWidget(controlsBox);
setLayout(mainLayout);
// Customize window
setWindowTitle(tr("Checkbuttons table"));
resize(1000, 1000);
//==============Pop-up section=================
// Create the popup window
popup = new ProfileCreator();
// Set the Qt::Popup window flag
// NOTE: You can set flags only here, in the mainwindow where you instantiate popup class
// otherwise: popup don't close correctly
popup->setWindowFlags(windowFlags() | Qt::Popup);
// Connect the createP button's clicked signal to the popup window's show slot
QObject::connect(createP, &QPushButton::clicked, this, [&]() { popup->show(); });
//<--- everything is good whith connecting 'createP' button and showing popup dialog
// Connect create button from popup dialog to the addRow() method
QObject::connect(popup, &ProfileCreator::createProfileClicked, this, [&]() { qDebug() << "you pressed"; });
//<--- PROBLEM here. No instance of connect method for such actions
// if I could simply log that I pressed a QDialog's button that would be enough for now
}
MainWindow::~MainWindow()
{
}
因此在ProfileCreator
类中继承QDialog
类。在头文件中:
#ifndef PROFILECREATOR_HPP
#define PROFILECREATOR_HPP
#include <QDialog>
#include <QPushButton>
#include <QLineEdit>
class ProfileCreator : public QDialog
{
Q_OBJECT
signals:
void createProfileClicked();
public:
ProfileCreator();
QPushButton* create;
};
#endif // PROFILECREATOR_HPP
在实现文件中:
#include "profilecreator.hpp"
#include <QVBoxLayout>
ProfileCreator::ProfileCreator()
{
// Set the size of the popup window
this->resize(800, 1200);
create = new QPushButton("Create");
// Set the size policies of the widgets to Fixed
create->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// Create a layout for the widgets
QVBoxLayout* layout = new QVBoxLayout();
// Add the widgets to the layout
layout->addWidget(create);
layout->addStretch();
// Set the layout of the popup window
this->setLayout(layout);
// Emit signal of pushing the create new profile button when it pressed
QObject::connect(create, &QPushButton::clicked, this, [&]() { emit createProfileClicked(); });
//<--- So with previous line I want to send a signal that QDialog's button is clicked.
//In the MainWindow constructor class I connect to such signal
}
在主.cpp中:
#include "mainwindow.hpp"
#include <QApplication>
int main(int argc, char* argv[])
{
// Invoke application, just to appear everything else
QApplication app(argc, argv);
// Here we keep all widgets
MainWindow window;
// Use the following if you want to have the app open as maximized window:
window.setWindowState(Qt::WindowMaximized);
// Show window to application
window.show();
return app.exec();
}
所以我所做的和我期望通过几个步骤得到的是:
1.我创建了一个继承自QDialog
的ProfileCreator
类作为弹出窗口;
1.在ProfileCreator
类中,我有QPushButton* create
;
1.当我在ProfileCreator
对象中按创建键时,将发出createProfileClicked
信号。
1.在MainWindow
类中,我初始化了一个指向ProfileCreator
对象“popup”的指针。
1.同样在MainWindow
类中,我创建了一个QPushButton* createP
。
1.我用弹出show()
方法连接createP &QPushButton::clicked
信号。弹出应该显示。〈---直到这一点没有问题。
1.我还想从ProfileCreator
连接一个QPushButton* create
到lambda:[&](){qDebug() << "you pressed";}
。记录消息-用于启动。
但是我好像不知道如何做步骤7的连接。
那么,谁能告诉我如何将一个类中弹出窗口的按钮与另一个类中的插槽连接起来呢?我知道,如果在MainWindow
的构造函数中创建弹出窗口,连接起来就不会有问题。但是我想组织我的代码,并将MainWindow
与ProfileCreator
弹出窗口分开。
1条答案
按热度按时间u91tlkcl1#
您的代码中未定义第一个
control
变量,您可以使用以下命令进行修复:关于连接问题(“步骤7”),
popup
的类型为QDialog *
,因此您的自定义信号不能用作QDialog
: