[英]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. 已解决的问题 :Q_OBJECT宏是必需的,正确的信号插槽声明对于任何其他句柄也很重要。
I am unable to focus on any input type widgets like QTextEdit
, QListWidget
etc. 我无法专注于任何输入类型的小部件,例如QTextEdit
, QListWidget
等。
Note : There are no compile time or runtime errors. 注意 :没有编译时或运行时错误。
Update : QSplitter
is working properly! 更新 : QSplitter
工作正常! I have a QListWidget
, whose items I click but they are highlighted only when I make the next move with the splitter. 我有一个QListWidget
,我单击了它的项目,但是只有当我使用拆分器进行下一步移动时,它们才会突出显示。
I have a MainWindow
class derived from QMainWindow
as declared in main_window.h
: 我有一个MainWindow
从派生类QMainWindow
在声明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
: 我还有另一个叫Stack
类,它是从stack.h
定义的QWidget
stack.h
:
class Stack: public QWidget{
public:
Stack(QWidget *parent=0);
//some other members
}
Constructor of Stack
as in stack.cpp
: Stack
构造方法如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. 如果我使用test->show()
在与MainWindow
分开的单独窗口中打开此小部件,则一切按预期/按我的方式工作。 But doing this in the MainWindow
constructor, renders it unclickable. 但是在MainWindow
构造函数中执行此操作会使它不可单击。
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? 为什么我无法集中处理任何可以接受输入的小部件,例如QTextEdit
, QListWidget
或单击任何QPushButton
小部件?
Please compile following code, it was working..you are getting focus and edit on QTextEdit... 请编译以下代码,它正在工作..您正在集中精力并在QTextEdit上进行编辑...
stack.h 栈
#include <QWidget>
class Stack: public QWidget
{
Q_OBJECT
public:
Stack(QWidget *parent = 0);
~Stack(void);
};
stack.cpp 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 主窗口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
mainwindow1.cpp 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 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.