关于Qt加载ui资源的问题

十秒耿直拆包选手 2018-01-23 05:16:47
在Qt项目创建了一个ui及配套的c++文件,
发现一个问题,当用new创建窗体时,其中的属性,方法可以正常调用(我在构造函数中创建信号槽,触发按钮可以正常调用),但是用QUiLoader加载ui时,其中的方法是无法调用的。请问这是为什么?
new创建窗体:
void MainWindow::GoToCell()
{
if ( !dialog_) {
dialog_ = new GoToCellDialog(this);
} else {
dialog_->SetText("");
}

if (dialog_->exec()) {

}
}


通过QUiLoader加载ui
void MainWindow::LoadUi()
{
QFile file(":/ui/gotocelldialog.ui");

if ( !file.open(QFile::ReadOnly)) {
return;
}

QUiLoader loader;
GoToCellDialog* dialog = (GoToCellDialog*) loader.load(&file, this);

if (dialog->exec()) {

}

delete dialog;
}



代码在百度云盘:https://pan.baidu.com/s/1ggX5KaR 提取码:vw6d


...全文
1681 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
donwmufromdying 2018-01-31
  • 打赏
  • 举报
回复
void Spreadsheet::Paste()
{
    QTableWidgetSelectionRange range = SelectedRange();
    QString content = QApplication::clipboard()->text();
    QStringList rows_content = content.split('\n');
    const int number_rows(rows_content.count());
    const int number_columns(rows_content.first().count('\t') + 1);

    if ((1 != (range.rowCount() * range.columnCount())) &&
            (range.rowCount() != number_rows || range.columnCount() != number_columns)) {
        QMessageBox::information(this,
                                 tr("Spreadsheet"),
                                 tr("The information cannot be pasted because the copy "
                                    "and paste areas aren't the same size."));
        return;
    }

    for (int row=0; number_rows != row; ++row) {
        QStringList columns_content = rows_content[row].split('\t');

        for (int column=0; number_columns != column; ++column) {
            int i = range.topRow() + row;
            int j = range.leftColumn() + column;

            if (ROW_COUNT_ > i && COLUMN_COUNT_ > j) {
                SetFormula(i, j, columns_content[j]);
            }
        }
    }

    SomethingChanged();
}
donwmufromdying 2018-01-31
  • 打赏
  • 举报
回复
我看了你的代码,除了语法错误外,用QUiLoader方式调用对话框共有成员函数没任何问题

void MainWindow::LoadUi()
{
    QFile file(":/ui/gotocelldialog.ui");

    if ( !file.open(QFile::ReadOnly)) {
        return;
    }

    QUiLoader loader;
    GoToCellDialog* dialog = (GoToCellDialog*) loader.load(&file, this);

    if (dialog->exec()) {
        dialog->SetText("Ok");
    }

    delete dialog;
}

16,229

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧