0

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 e.g. QTextEdit,QListWidget or click any QPushButton widget?

4

2 に答える 2

0

次のコードをコンパイルしてください。動作していました..QTextEditにフォーカスして編集しています...

stack.h

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

スタック.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)
    {
    }

メインウィンドウ1.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

メインウィンドウ1.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();
}
于 2013-08-20T10:39:34.267 に答える