我对Qt有意见。
TL;DR
我的设计器窗体类继承自QWidget,并包含一个按钮。我用父参数(即MainWindow对象)从这个类构造一个对象。小部件显示,但不能单击按钮,对鼠标悬停没有反应,但当按钮被选项卡键突出显示并按下空格时,会触发类似于onclick的事件。
以下是我的主要功能:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow *w;
if(argc == 2)
{
w = new MainWindow(argv[1]);
}
else
{
w = new MainWindow();
}
w->show();
return a.exec();
}
以下是MainWindow的来源:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow (parent),
ui(new Ui::MainWindow),
todoWidget(nullptr)
{
ui->setupUi(this);
todoWidget = new TodoWidget(nullptr, this);
todoWidget->setEnabled(true);
}
MainWindow::MainWindow(const char *saveFilePath, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
todoWidget(new TodoWidget(saveFilePath, this))
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
delete todoWidget;
}
todoWidget是MainWindow类的私有成员。我理解,如果在QWidget的构造函数中给出了父部件,那么小部件将在父部件中绘制。这种情况发生了,但是小部件中的按钮是不可点击的。如果我按标签,直到它在焦点上,而不是按下空格,我就能触发类似于onclick之类的东西,但它甚至对鼠标悬停都没有反应。TodoWidget类是一个设计器表单类,现在我将它简化为只有一个元素,即按钮。下面是TodoWidget的源代码:
TodoWidget::TodoWidget(const char *path, QWidget *parent) :
QWidget (parent),
t (nullptr),
ui(new Ui::TodoWidget)
{
ui->setupUi(this);
if(!path)
{
path = "./tasks/taks";
}
open(path);
makeConnections();
todoModel = new TaskListModel(t->tasksWithStatus(Task::Status::TODO), this);
inProgressModel = new TaskListModel(t->tasksWithStatus(Task::Status::IN_PROGRESS), this);
finishedModel = new TaskListModel(t->tasksWithStatus(Task::Status::FINISHED), this);
// ui->todoView->setModel(todoModel);
// ui->inProgressView->setModel(inProgressModel);
// ui->finishedView->setModel(finishedModel);
}
TodoWidget::~TodoWidget()
{
delete t;
delete ui;
}
void TodoWidget::open(const char *path)
{
if(validateSaveFile(path))
{
delete t;
t = new todo(path, this);
this->path = path;
}
}
void TodoWidget::progressTask(unsigned int index)
{
t->progressTask(index);
}
void TodoWidget::displaySuccess(const QString &msg)
{
QMessageBox::information(this, "Success", msg);
}
void TodoWidget::displayError(const QString &errMsg)
{
QMessageBox::critical(this, "Error", errMsg);
}
void TodoWidget::changeProject(const char *path)
{
open(path);
}
void TodoWidget::addButtonClicked()
{
AddWindow *addWindow = new AddWindow(this);
connect(addWindow, &AddWindow::addTask, [this](const QString &args)->void{addTask(args);});
addWindow->show();
}
bool TodoWidget::validateSaveFile(const QString &path)
{
/*
* C:/ and (wordchar 0 or more times/) 0 or more times
* ./ and (wordchar 0 or more times/) 0 or more times
* / and (wordchar 0 or more times/) 0 or more times
* (wordchar 0 or more times/) 0 or more times
*
* Note: the parentheses are not present in the regexp
* Note: wordchar is any character which might be part of a word (alfanum and _ I think)
*/
QRegularExpression regexp("([a-zA-Z]:/|[.]{0,2}/)?((.*)/)*");
QRegularExpressionMatch match = regexp.match(path);
if(!match.hasMatch())
{
emit someError("Not a valid file");
return false;
}
else
{
QDir sp(match.captured(0));
if(!sp.exists())
{
sp.mkpath(".");
}
}
QFile file(path);
if(!file.open(QIODevice::Append | QIODevice::Text))
{
emit someError(QString("Can't open file") + file.errorString());
return false;
}
return true;
}
void TodoWidget::makeConnections()
{
connect(t, &todo::taskAdded, this, &TodoWidget::displaySuccess);
connect(t, &todo::taskNotAdded, this, &TodoWidget::displayError);
connect(t, &todo::taskMadeProgress, this, &TodoWidget::displaySuccess);
connect(t, &todo::noProgress, this, &TodoWidget::displayError);
connect(t, &todo::saved, this, &TodoWidget::displaySuccess);
connect(t, &todo::notSaved, this, &TodoWidget::displayError);
connect(this, &TodoWidget::addTask, t, &todo::addTask);
connect(this, &TodoWidget::someError, this, &TodoWidget::displayError);
connect(this->ui->addButton, &QPushButton::clicked, this, &TodoWidget::addButtonClicked);
}
void TodoWidget::modelStuff()
{
}
T是TodoWidget的私有成员。类型是待办的。我不认为有必要显示代码,因为它应该与GUI无关,因为它是一个共享库(我用Qt创建的)。
我已经试过好几件事了,我甚至都记不起来了,但都没有用。我会感谢你的帮助。谢谢
发布于 2019-03-21 14:02:06
mainWindow以中央小部件停靠区、状态栏、菜单栏etc.So的形式预定义了布局--任何小部件都需要设置到这些区域中才能正常工作。尝试使用call mainWindow.setcentralwidget(todowidget)设置对话框。
https://stackoverflow.com/questions/55282034
复制相似问题