gpt4 book ai didi

c++ - Qt:访问由 uitools/uiloader 生成的小部件的信号/插槽

转载 作者:行者123 更新时间:2023-11-30 04:01:27 26 4
gpt4 key购买 nike

我的应用程序显示了一个 MainWindow 用户界面,它在运行时获得了新的用户界面部分。主窗口的用户界面是在编译时创建的,而新的小部件是在运行时加载的。 (在 qrc 和 uiloader 中有 .ui)

我的问题:尽管 Qt 报告 QObject::connect() 全部成功,但我无法触发运行时小部件的信号和槽。

我在这里和其他地方阅读了很多关于信号和槽的故障排除建议(比如这些 20 tips ),但我开始怀疑这是使用 uitools/uiloader 获取运行时 ui 元素的独特之处。这是因为主窗口的信号和槽都可以工作。

我在 Windows 7 专业版上使用 Qt 5.2.1、Qt Creator 3.0.1。我也使用 boost 线程,没有 QThreads。

图说千言。

主窗口.ui

main window, contains a button and a text edit

子控件.ui

child widget, which looks a lot like main window, with an extra label

合并。

主窗口垂直布局内的 2 个子窗口小部件

combined, two child widgets sit inside the main window's Vertical Layout

在这个程序实例中,我在主窗口中硬编码了两个子窗口的生成。

我将主窗口的 PushButton 点击信号连接到一个 test() 插槽,它在主窗口的文本框中写入“OH PUSH”。相同的信号连接到所有子窗口的 test() 插槽,它们应该对所有的文本框执行相同的操作。没有任何显示。

程序中隐藏了一个带有自定义 QObject 的 Boost::Thread。它的信号每 2 秒触发一次,同样连接到所有 test() 槽。只有主窗口显示“OH PUSH”。

我还尝试将 Toggle Pause 按钮的点击与所有插槽相关联。在这种情况下,点击时甚至不会触发信号。

目前没有代码,因为我希望这是一个专业人士可以快速识别的简单问题。如果不是这样,我将显示代码。


编辑:在编写包含代码的编辑时,我无意中发现了解决方案。我将在此处显示文章并张贴解释为什么我的程序无法运行。

主要玩家有MainController、MainWindow、ChildController、ChildWidget

/*
main spawns a mainController.
mainController::ctor spawns a QApplication.
mainController::ctor spawns a MainWindow.
The MainWindow spawns its ui. ( `ui->setupUI( this );` )

// back in main, after spawning MainWindow
main calls mainController's init()
mainController::init() spawns a ChildController.
ChildController::ctor spawns a boost::thread that idles.
mainController::init() spawns a ChildWidget.* // see below for code
mainController::init() connects MainWindow with all ChildWidgets, as well as all signals/slots**. // see below for the code
mainController::init() loops to spawn another pair of ChildController/ChildWidget, if required.

// back in main, after calling mainController's init()
main calls mainController's run()
mainController::run() calls MainWindow's show()
mainController::run() calls (every) ChildController's unpause(), so the boost::threads stop idling and starts emitting signals every 2 seconds.
mainController::run() calls QApplication's exec() and blocks till the end of the program.
*/

*ChildWidget 已生成。

class ChildWidget : public QWidget
{
Q_OBJECT

public:
ChildWidget( QWidget* parent = 0 );

QWidget* get_pointer_to_ui();

public slots:
void slot_push_text( std::string text );
void slot_push_image( const std::vector< unsigned char > image );
void slot_test();

private slots:
void on_buttonPause_clicked();

private:
QPlainTextEdit* uiText;
QLabel* uiImage;
QPushButton* uiPauseButton;

QWidget* ui;

private:
QWidget* load_ui(); // called on construct
};

ChildWidget::ChildWidget( QWidget* parent )
: QWidget( parent )
{
QWidget* widg = load_ui();

uiImage = widg->findChild< QLabel* >( "labelImage" );
uiText = widg->findChild< QPlainTextEdit* >( "textConsole" );
uiPauseButton = widg->findChild< QPushButton* >( "buttonPause" );

ui = widg;

QMetaObject::connectSlotsByName( this );
}

QWidget* ChildWidget::load_ui()
{
QUiLoader loader;

QFile file(":/widgets/ChildWidget.ui");
file.open(QFile::ReadOnly);

QWidget *widg = loader.load(&file, this);
file.close();

return widg;
}

**主 Controller 连接所有的信号和槽。

void MainController::init()
{
//FIXME. hard code to spawn two for now.
for( int i = 0; i < 2; ++i )
{ // routine spawning the child controllers

/* **** construct the child controllers, which internally contains a small module that emits sigs **** */
boost::shared_ptr< ChildController > v1 = boost::shared_ptr< ChildController >( new ChildController );

/* **** each child controller is to have 1-1 corresponding ui element. **** */
/* We shall spawn ONE ChildWidget for each ChildController.
* Each ChildWidget itself contains pointer to a widget generated via runtime uic.
* This widget needs to be passed to The Main Controller so it can be added to its layout.
*/
boost::shared_ptr< ChildWidget > newWidget = boost::shared_ptr< ChildWidget >( new ChildWidget( w.get() ) );
// a new ChildWidget is created, whose parent is defined to be the MainWindow, w

// the main window will now attempt to incorporate the new widget in its layout
QWidget* newWidgUi = newWidget->get_pointer_to_ui(); // dangerous? returns a pointer to the ui loaded with `loader.load(&file, this)` above
this->w->add_view( newWidgUi ); // calls MainWindow's VerticalLayout addWidget()


// all signals and slots are to be connected next.
if( newWidget != 0 )
{
// if main window button pressed, send text to this ChildWidget's text window
bool isTest = QObject::connect( w.get(), &MainWindow::sig_test,
newWidget.get(), &ChildWidget::slot_push_text );

// if main window button pressed, send text to the main window's text window.
QObject::connect( w.get(), &MainWindow::sig_test,
w.get(), &MainWindow::slot_test );

// if the boost::thread signals, deliver some text to this ChildWidget
QObject::connect( v1->widget.get(), &ChildController::sig_push_text,
newWidget.get(), &ChildWidget::slot_push_text );

// if this child window's button is pressed, show some text on the main window.
QObject::connect( v1->widget.get(), &ChildController::sig_push_text,
this->w.get(), &MainWindow::slot_test );

this->vControllers.push_back( v1 );
}

}

// all the connections above succeed.
// but this is where my problem was.
// newWidget was not stored beyond this function, so boost::shared_ptr destroys it
}

杂项说明:我在 MainController.cpp 中完成了以下操作

Q_DECLARE_METATYPE( std::string )
Q_DECLARE_METATYPE( std::vector< unsigned char > )

并添加

qRegisterMetaType< std::string >( "std::string" );
qRegisterMetaType< std::string >( "std::vector< unsigned char >" );

在 MainController 的构造函数中。

我把它放在 main() 的第一行

Q_INIT_RESOURCE( dynwidgets ); // dynwidgets.qrc contains the ChildWindow's .ui file

起初,我把代码放在这里,但它变得非常困惑,所以现在它只是感兴趣区域的一个片段,其余部分是描述性文本。

最佳答案

原来我犯了一个与 Qt 无关的基本错误。

当通过 QUiLoader 生成 ui 元素时,我伴随它生成了一个伴随小部件。此小部件用于从 ui 发出信号,并在其槽被触发时对 ui 进行更改。

所有连接都已正确建立,但不会仅仅因为伴随小部件被意外删除,使所有连接失效而触发。

  • 混合编译时 ui 和运行时加载 ui 是完全有效的
  • 在运行时生成的信号和槽的行为方式与任何其他方式相同
  • 仔细管理小部件的生命周期

关于c++ - Qt:访问由 uitools/uiloader 生成的小部件的信号/插槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25736909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com