简体   繁体   中英

Unable to focus and give input to widgets in Qt5

Issue Resolved : Q_OBJECT macro was necessary and proper signal slot declarations are also important for any other handles.

I am unable to focus on any input type widgets like QTextEdit , QListWidget etc.

Note : There are no compile time or runtime errors.

Update : QSplitter is working properly! I have a QListWidget , whose items I click but they are highlighted only when I make the next move with the splitter.

I have a MainWindow class derived from QMainWindow as declared in main_window.h :

class MainWindow : public QMainWindow{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
//some other members like menu and statusbar here
}

I have another class called Stack derived from QWidget defined in stack.h :

class Stack: public QWidget{
public:
    Stack(QWidget *parent=0);
//some other members
}

Constructor of Stack as in stack.cpp :

 Stack::Stack(QWidget *parent):QWidget(parent){
        main = new QHBoxLayout;
        handle = new QSplitter;
        setupList();
        setupScreens();
        //above functions add the widgets to the handle splitter
        main->addWidget(handle);
        setLayout(main);
    }

If i open up this widget in a separate window from the MainWindow using test->show() , the things work as expected/as i want. But doing this in the MainWindow constructor, renders it unclickable.

MainWindow::MainWindow(QWidget *parent):QMainWindow(parent){
    Stack *test = new Stack(this);
    //test->show();
    setCentralWidget(test);
}

This is strange. Why am i not able to focus any widget that can take input eg QTextEdit , QListWidget or click any QPushButton widget?

Please compile following code, it was working..you are getting focus and edit on QTextEdit...

stack.h

#include <QWidget>
class Stack: public QWidget
{
    Q_OBJECT
public:
    Stack(QWidget *parent = 0);
    ~Stack(void);
};

stack.cpp

   #include "Stack.h"
    #include<QTextEdit>
    #include<QHBoxLayout>

    Stack::Stack(QWidget *parent):QWidget(parent){
           QHBoxLayout* main = new QHBoxLayout;
            QTextEdit *test = new QTextEdit;

            main->addWidget(test);

            //other things added to main layout
            setLayout(main);
        }
    Stack::~Stack(void)
    {
    }

mainwindow1.h

  #ifndef MAINWINDOW1_H
    #define MAINWINDOW1_H

    #include <QtGui/QMainWindow>
    //#include "ui_mainwindow1.h"

    class Mainwindow1 : public QMainWindow
    {
        Q_OBJECT

    public:
        Mainwindow1(QWidget *parent = 0, Qt::WFlags flags = 0);
        ~Mainwindow1();

    private:
        //Ui::Mainwindow1Class ui;
    };

    #endif // MAINWINDOW1_H

mainwindow1.cpp

  #include "mainwindow1.h"
    #include "Stack.h"
    #include <QTextEdit>

    Mainwindow1::Mainwindow1(QWidget *parent, Qt::WFlags flags)
        : QMainWindow(parent, flags)
    {
        Stack *test = new Stack;
        setCentralWidget(test); 
    }

    Mainwindow1::~Mainwindow1()
    {

    }

main.cpp

#include "mainwindow1.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Mainwindow1 w;
    w.show();
    return a.exec();
}

如果some1可以找到有关如何在QT5中从UI设置对输入小部件的关注的答案,则可以使用:

ui->plainTextEdit->setFocus();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM