Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Qt signals and slots in different classes
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Qt signals and slots in different classes

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 766 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Spike89
    wrote on last edited by
    #1

    I made a public slot in this class:
    @class open : public QObject
    {
    Q_OBJECT

    public:
    open();
    QString filename;
    QByteArray ba;
    public slots:
    void openfile(){
    filename=QFileDialog::getOpenFileName(
    0,
    QObject::tr("Open File"),
    ("C://"),
    ("BITMAP(*.bmp)")
    );
    QFile file(filename);

            file.open(QIODevice::ReadOnly);
            ba=file.readAll();
    
    
    }
    

    };@
    I want to connect this slot to my pushbutton's signal in the mainwindow.cpp like this:
    @open x;
    connect(ui->pushButton,SIGNAL(clicked()),
    &x,SLOT(openfile()));
    @
    But nothing happend when i pushed the button. What am is doing wrong?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You create "x" on stack and without a parent. It will be destroyed when it goes out of scope, and so it will not respond to the signal.

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Spike89
        wrote on last edited by
        #3

        [quote author="sierdzio" date="1397126276"]You create "x" on stack and without a parent. It will be destroyed when it goes out of scope, and so it will not respond to the signal.[/quote]

        Thank you.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved