如何读取和写入文本文件日期进入QTableView?
这就是我所拥有的,但我希望在将数据添加到表中时保存数据,当然可以在重新打开应用程序时将其读回.有没有我可以参考的教程?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QStandardItemModel();
model->setRowCount(0);
ui->tableView->setModel(model);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QStandardItem *userName = new QStandardItem(ui->lineEdit_Name->text());
QStandardItem *userNumber = new QStandardItem(ui->lineEdit_Number->text());
QList<QStandardItem*> row;
row <<userName << userNumber;
model->appendRow(row);
}
Run Code Online (Sandbox Code Playgroud)
非常感谢
编辑--------------------------------
这对我有用:
添加功能:
void MainWindow::on_pushButto_Add_clicked() {
QStandardItem *userInput = new QStandardItem(ui->lineEdit->text());
QStandardItem *userInput2= new QStandardItem(ui->lineEdit_2->text());
QList<QStandardItem*> row;
row <<userInput << userInput2;
model->appendRow(row);
}
Run Code Online (Sandbox Code Playgroud)
保存功能:
void MainWindow::on_pushButton_Save_clicked()
{
QFile file("C:/Users/UserName/Practicing/Resources_Files/someFile.bin");
if (file.open(QIODevice::WriteOnly))
{
QDataStream stream(&file);
qint32 n …
Run Code Online (Sandbox Code Playgroud) 如何从plist中将值附加到NSDictionary中的现有键?
我所拥有的基本上是一个保存在光盘中的带有几个键和值的plist,键是一些具有一个初始值的学生的名字.我试图做的不起作用是通过读取plist将更多项添加到现有键/学生,将其添加到临时NSDictionary,将临时数组附加到plist中的现有键但是当我保存时plist back它不起作用,它只保存最后两项并基本上删除该键的初始值.
这是我正在使用的代码......
- (IBAction)testing:(id)sender
{
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [dirPaths objectAtIndex:0];
NSString *fullPath = [documentsPath stringByAppendingPathComponent:@"studentsRecords.plist"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];
if(fileExists)
{
NSMutableDictionary *dictionaryRecords = [[NSMutableDictionary alloc]initWithContentsOfFile:fullPath];
NSLog(@"Items for Nathan: %@",[dictionaryRecords objectForKey:@"Nathan"]);// here the output is, Items for Nathan: Ruler
// which make sense since I only had one initial record for Nathan in my plist,
// Here, I want to add two more items to the key Nathan by appending …
Run Code Online (Sandbox Code Playgroud) 我有一个UITableView
包含标签的自定义单元格.我想要的是能够在选择行时将此标签的内容传递给第二个视图控制器.
我在没有自定义单元格的表格上运行的代码很好:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell
//labelDisplayLedQty is the name of the custom label
cell.labelDisplayLedQty.text = inputLedQty.text
return cell
}
Run Code Online (Sandbox Code Playgroud)
这是我在该prepareForSegue
方法中的代码.正如我所说,如果我将它与不包含自定义单元格的表一起使用,此代码可以正常工作.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let rowIndex: NSIndexPath = tableList.indexPathForSelectedRow!
let selectedRow: UITableViewCell = tableList.cellForRowAtIndexPath(rowIndex)!
let contentFromSelectedRow: String = selectedRow.textLabel!.text!
if let secondVC = segue.destinationViewController as? DriverDetailsViewController where
segue.identifier == "segueDriverDetails" {
secondVC.messageContentFromMainController = contentFromSelectedRow + " This message is …
Run Code Online (Sandbox Code Playgroud) 我正在努力提高我的面向对象技能,而且我总是在争论是否需要课程.
我有一组UIColors,我一直在不同的ViewControllers中使用我的应用程序,我最初开始时将它们添加为常量全局,如下所示...
import UIKit
// Global COLORS
let myBlueColor = UIColor(red: 62.0/255, green: 174.0/255, blue: 206.0/255, alpha: 1.0)
// more global colors here...
class ViewController1{
// using my global color
myButton.layer.borderColor = myBlueColor.CGColor
}
// other viewController
import UIKit
class ViewController2{
// using my global color again
myButton2.layer.borderColor = myBlueColor.CGColor
}
Run Code Online (Sandbox Code Playgroud)
但后来我决定创建一个类来强迫自己以更加面向的方式思考......
颜色类
import Foundation
import UIKit
class Color {
var myBlueColor:UIColor{
get{
return UIColor(red: 62.0/255, green: 174.0/255, blue: 206.0/255, alpha: 1.0)
}
}
var myLightGrayColor:UIColor{
get{
return UIColor(red: 249.0/255, …
Run Code Online (Sandbox Code Playgroud) 我有一堆按钮,UIToolbar
当我UITextField
点击使用时,我正在添加这些按钮inputAccessoryView
.所有这些按钮都是相同的标题以外的什么,我想能够做的就是重用UI属性,如titleColor
,frame
等.
什么是完成我上面描述的最有效的方法?
这是代码......
// code for first button
let button1 = UIButton();
button1.backgroundColor = UIColor.orangeColor()
button1.setTitle("button1", forState: .Normal)
button1.setTitleColor(UIColor.whiteColor(), forState: .Normal)
button1.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
button1.frame = CGRect(x:0, y:0, width:35, height:35)
button1.addTarget(self, action: #selector(myFunction), forControlEvents: UIControlEvents.TouchUpInside)
// code for second button which is identical to the first button
let button2 = UIButton();
button2.backgroundColor = UIColor.orangeColor()
button2.setTitle("button2", forState: .Normal)
button2.setTitleColor(UIColor.whiteColor(), forState: .Normal)
button2.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
button2.frame = CGRect(x:0, y:0, width:35, height:35)
button2.addTarget(self, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试从中捕获所选项目UITableView
并将其保存到新数组中。下面的代码通过在点击行时添加项目来创建新数组,而当取消选择行时,它不执行的操作是删除项目。
newFruitList
当UITableView
取消选择a中的行时,如何从中删除项目?
还是更好的方法是,仅生成一个选定项目的数组的正确方法是UITableView
什么?
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let fruits = ["Apples", "Oranges", "Grapes", "Watermelon", "Peaches"]
var newFruitList:[String] = []
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fruits.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = fruits[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { …
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我正在检查某个项目是否存在于 an 中ObservableCollection
,如果存在,我想获取它的索引,以便我可以将该项目移动到顶部,但我无法弄清楚。
我的对象:
public class RecentFile
{
public string FileName { get; set; }
public string DateAdded { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
用于查找项目索引的代码:
if (recentFilesObservableCollection.Any(f => f.FileName == "MyFileName")) {
foreach (RecentFile file in recentFilesObservableCollection) {
if (file.FileName == "MyFileName") {
var x = RecentFilesDataGrid[(recentFilesObservableCollection.IndexOf(file)];
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果项目存在,获取索引号的最佳方法是什么?
最终我需要做的是......
如何重新映射快速上下命令?
我想重新映射Ctrl-U到Ctrl-K和Ctrl-D到Ctrl-J?
我试过以下但没有运气......
nnoremap <c-u> <c-k>
nnoremap <c-d> <c-j>
Run Code Online (Sandbox Code Playgroud)
仅供参考 - Vim安装在Ubuntu Server中
在下面的代码中我有一个动作/按钮,当点击时带你到另一个viewController(SecondViewController
),一切正常,但有一行我不完全理解的代码.
这行代码在做什么?
secondVC.delegate = self
我们在这里谈论的代表是什么?如果我只需要转到其他视图并且我没有传递任何数据,那真的需要吗?
@IBAction func goToView(sender: AnyObject) {
let secondVC= storyboard?.instantiateViewControllerWithIdentifier("secondViewController") as! SecondViewController
secondVC.delegate = self // is this needed?
self.presentViewController(secondVC, animated:true, completion:nil)
}
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我正在尝试格式化Double
,NumberFormetter
但我得到的输出与其他应用程序中的输出不匹配.
这是我正在使用的代码......
let price:Double = 25
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = NSLocale.current
let priceFormatter = currencyFormatter.string(for: price)
myLabel.text = priceFormatter
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,并且Region
在我的应用程序中将手机设置为中国,我得到的25,00 CNY
是另外两个第三方应用程序的输出CN¥25.00
.
我究竟做错了什么?
我的应用程序
25,00 CNY其他应用程序
¥25.00
我的应用程序
25,00€其他应用程序
€25,00
但是当该区域设置为United States
我在所有三(3)个应用程序中得到相同的表示法.
我的应用程序
$ 25.00其他应用
$ 25.00
我有点担心尤其是当区域设置为中国的输出,因为除了符号是不同的我也得到了不同的符号CNY
VS CN¥
.
有什么建议,我做错了吗?
ios ×6
swift ×6
uitableview ×2
c# ×1
delegates ×1
nsdictionary ×1
objective-c ×1
oop ×1
plist ×1
qmodelindex ×1
qt ×1
qtableview ×1
segue ×1
uibutton ×1
uitextfield ×1
uitoolbar ×1
vim ×1