我在表单中有一个QListWidget
,其中一个QListWidgetItem
显示“ 添加新”。当我单击它时,我希望发生一系列事情:
QInputDialog::getText
询问新项目的内容。最后一部分是我遇到的麻烦。我尝试了许多不同的方法,所有这些方法都导致相同的结果:我要选择的项目带有虚线边框,并且被理解为选中状态(例如,用ui->list->selectedItems()
表示,但是选择颜色保留在之前的最后一个项目上“ 添加新”。
item->setSelected(true);
ui->list->setCurrentItem(item);
ui->list->setCurrentRow(ui->list->row(item);
当调试器在断点处缓慢运行时,我注意到一切似乎都正常运行,但是UI并没有在我调用的函数完成之前更新。
此外,当我想从另一个按钮单击调用的插槽中从列表中选择给定项目时,它可以与item->setSelected(true);
(以及其他按钮)一起正常使用。
我的猜测:由于无法以图形方式选择尚不存在的内容,因此无法在添加该功能的同时选择该项目。
有人猜想如何实现这一目标吗?
答案 0 :(得分:0)
如果您可以从常规插槽中选择项目,那么只需从非常短的计时器中发出虚拟信号即可。像这样
//add item
//...
QTimer::singleShot(1, this, SLOT(MySlotForSelectItem())); // 1 ms timer
MainWindow::MySlotForSelectItem()
{
//select item
}
答案 1 :(得分:0)
您尝试选择添加的项目,然后将当前行设置为添加项目的行索引。这在我的示例中有效。
示例:mainwindow.cpp
import os
import pandas as pd
from keras.models import Sequential
from keras.layers import *
from sklearn.preprocessing import MinMaxScaler
import sys
np.set_printoptions(threshold=sys.maxsize)
# Load training data set from CSV file
training_data_df = pd.read_csv("freedom_data_training1.csv", dtype=float)
# Selecting columns for prediction
training_data_df = training_data_df.iloc[:, 0:5]
# Converting into 2D array
training_data_df = training_data_df.values
# Scaling of Data Normalization
sc = MinMaxScaler()
training_data_df = sc.fit_transform(training_data_df)
# Creating Training Data with Timesteps
X_train = []
Y_train = []
for i in range(5, len(training_data_df)):
X_train.append(training_data_df[i-5:i, 0:4])
Y_train.append(training_data_df[i, 4])
X_train, Y_train = np.array(X_train), np.array(Y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 4))
#RNN
regressor = Sequential()
# Adding the input layer and the LSTM layer
regressor.add(LSTM(units=4, activation='relu', input_shape=
(X_train.shape[1], 4)))
# Adding the output layer
regressor.add(Dense(units=1))
# Compiling the Recurrent Neural Network
regressor.compile(optimizer='adam', loss='mean_squared_error')
# Fitting the Recurrent Neural Network
regressor.fit(X_train, Y_train, batch_size=10, epochs=10)
# Load training data set from CSV file
testing_data_df = pd.read_csv("freedom_data_test1.csv", dtype=float)
#Selecting columns
testing_data_df = testing_data_df.iloc[:, 0:5]
# Converting into 2D array
testing_data_df = testing_data_df.values
# Scaling of Data
sc = MinMaxScaler()
testing_data_df = sc.fit_transform(testing_data_df)
# Creating Testing Data with Timesteps
X_test = []
Y_test = []
for i in range(5, len(testing_data_df)):
X_test.append(testing_data_df[i-5:i, 0:4])
Y_test.append(testing_data_df[i, 4])
X_test, Y_test = np.array(X_test), np.array(Y_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 4))
prediction = regressor.predict(X_test)
f = open("prediction_v1", "w+")
print(prediction, file=f)
f.close()