سؤال

أريد تنفيذ أوامر بسيطة مثل qDebug() عندما أنقر على قائمة فرعية في النافذة الرئيسية.كنت أشير إلى نموذج البرنامج المقدم مع Qt 5 IDE (...\Qt\Qt5.2.0\5.2.0\msvc2010\examples\widgets\mainwindows\menus)، وباستخدامه، تمكنت من إنشاء الكود.لا أتلقى أي وقت ترجمة أو أخطاء وقت التشغيل.

لقد قمت بإنشاء mainwindow.ui باستخدام وضع التصميم.يحتوي على كائن من فئة QAction يسمى actionInterval. snapshot of UI, which requires action when I click on Interval button

ولكن عندما أنقر عليه، لا يحدث شيء، ولا أستطيع تنفيذ الأمر في الفاصل الزمني الفارغ ().أعتقد أنني لا أتصل بشكل صحيح.ما الذي أفتقده هنا؟يرجى تقديم النصيحة.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


private:
    Ui::MainWindow *ui;
    void createActions();

private slots:
    void interval();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    createActions();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::createActions()
{
    ui->actionInterval = new QAction(tr("&Interval"), this);
    ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
    connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
}

void MainWindow::interval()
{
    qDebug()<<"inside interval qdialog";
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
هل كانت مفيدة؟

المحلول

void MainWindow::createActions()
{
    ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
    connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
}

لا ينبغي أن تحتاج إلى ذلك ui->actionInterval = new QAction(tr("&Interval"), this); الخط، ui->setupUi() يتعامل مع ذلك نيابةً عنك، لذلك من المحتمل أن يتسبب في مرجع غير صحيح، لذا عندما تنقر عليه، لا يتم تشغيله بشكل صحيح.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top